The following pseudo-code describes a menu-driven algorithm:
loop
ask the user to input one of the characters a, b, c, d, e, q read in a character from the keyboard
if the character is
'a' output your name and your tutor's name (hard-coded) 'b' input 3 double numbers x, y, z and output the largest
and the smallest of the three numbers
'c' input 2 integer numbers m and n, and display all the
numbers between m and n (both inclusive) with five numbers per line (note that the last line may have less than 5 numbers). At the end, display the sum of all the odd numbers between m and n (both inclusive)
'd' input 3 integer numbers representing the sides of a triangle and display the numbers together with a message indicating whether or not the numbers form a triangle (Note: for the numbers to form a triangle, the sum of any two sides must be greater than the third side)
'e' input an integer number n and determine whether or not n is a prime number (Note: a number is a prime number if it is greater than 1 and has no divisors other than
1 and itself)
'q' exit from the loop
other: output an error message end if
end loop
1. Implement the above pseudo-code in a Java program using a while loop and a switch-case statement. The program should be well structured, and the task performed under each option (at least options 'b' to 'e') should be implemented as a separate method.
NOTE: The Scanner class does not have a method to input a character. In order to read a character from the keyboard, use one of the following methods (after declaring the Scanner object):
static Scanner sc = new Scanner(System.in);
1. char ch = sc.nextLine().charAt(0); OR
2. char ch = sc.nextLine().toLowerCase().charAt(0);
where sc is a Scanner class object.
The second method above also converts the input to lowercase, which is often useful. Though these methods allow the user to input more than one character on the input line, the rest of the line (after capturing the first character withcharAt(0)) is discarded.
If you also want to ignore the leading spaces before the first character then use:
1. char ch = sc.nextLine().trim().charAt(0); OR
2. char ch = sc.nextLine().trim().toLowerCase().charAt(0);
2. Write a program to extend question 1, by adding an option 'f' to your menu program. If this option is chosen, the program should ask the user to enter 10 integers. The program should store each integer into an array (as each one is entered). Once the array contains the 10 integers, the program should output: a) The sum of the 10 numbers entered
b) The average of the 10 numbers entered c) The highest number entered
d) The lowest number entered
3. Write a program that gets a string from the user and reports whether or not there are repeated characters in it.
4. Write a program that gets a string from the user and swaps the first and last characters.
/* * Question #1*/import java.util.*;public class Menu { public static int largest(int x, int y, int z) { int large = x; if(x >= y && x >= z) large = x; else if(y>=x && y >=z) large = y; else large = z; return large; } public static int smallest(int x, int y, int z) { int small = x; if(x <= y && x <= z) small = x; else if(y<=x && y <=z) small = y; else small = z; return small; } public static void largeSmall() { Scanner sc = new Scanner(System.in); System.out.print("Enter three numbers: "); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); System.out.println("Largest Number is " + largest(x,y,z)); System.out.println("Smallest Number is " + smallest(x,y,z)); } public static void numList() { Scanner sc = new Scanner(System.in); System.out.print("Enter two numbers: "); int m = sc.nextInt(); int n = sc.nextInt(); int count = 0; int sumOdd = 0; int i; for(i=m; i<=n; ++i) { System.out.print(i + " "); ++count; if(count==5) { System.out.println(); count = 0; } if(i%2 != 0) sumOdd = sumOdd + i; } System.out.println(); System.out.println("Sum of odd numbers between " + m + " and " + n + " is " + sumOdd); } public static void isTriangle() { Scanner sc = new Scanner(System.in); System.out.print("Enter three sides of triangle: "); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); String res = ""; if(!((a+b) > c && (b+c) > a && (a+c) > b)) res = "not"; System.out.println("The three sides " + a + ", " + b + ", " + c + " can" + res + " form a triangle"); } public static void isPrime() { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int n = sc.nextInt(); int i; boolean prime = true; if(n<2) prime = false; else { for(i=2; i<(n/2); ++i) { if(n%i == 0) { prime = false; break; } } } if(prime) System.out.println(n + " is a Prime number"); else System.out.println(n + " is not a Prime number"); } public static void arrayNum() { Scanner sc = new Scanner(System.in); System.out.print("Enter ten numbers: "); int a[] = new int[10]; int i; int sum = 0; double avg = 0.0; int max, min; for(i=0; i<10; ++i) a[i] = sc.nextInt(); max = min = a[0]; for(i=0; i<10; ++i) { sum = sum + a[i]; if(a[i] > max) max = a[i]; if(a[i] < min) min = a[i]; } avg = sum/10.0; System.out.println("Sum of the 10 numbers is " + sum); System.out.println("Average of the 10 numbers is " + avg); System.out.println("Highest Number is " + max); System.out.println("Lowest Number is " + min); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); char ch; loop: do { System.out.print("Enter a character: "); ch = sc.next().toLowerCase().trim().charAt(0); switch(ch) { case 'a': System.out.println("MyName"); //Replace MyName with your name System.out.println("TutorName"); //Replace MyName with your tutor's name break; case 'b': largeSmall(); break; case 'c': numList(); break; case 'd': isTriangle(); break; case 'e': isPrime(); break; case 'f': arrayNum(); break; case 'q': break loop; default: System.out.println("Invalid Character. Please re-try OR type 'q' to exit"); break; } }while(ch!='q'); } }/* * Question #2*/import java.util.*;public class RepeatChar { public static void main(String []args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a String: "); String s = sc.nextLine(); int i; char ch; String res = "does not have"; for(i=0; i< s.length(); ++i) { ch = s.charAt(i); if(s.indexOf(ch) != s.lastIndexOf(ch)) { res = "has"; } } System.out.println("String " + res + " repeated characters"); } }/* * Question #3*/import java.util.*;public class SwapChar { public static void main(String []srgs) { Scanner sc = new Scanner(System.in); System.out.print("Enter a String: "); String s = sc.nextLine(); int i; int l = s.length(); String swap = ""; swap = swap + s.charAt(l-1); for(i=1; i<l-1; ++i) { swap = swap + s.charAt(i); } swap = swap + s.charAt(0); System.out.println("String after swapping first and last character is " + swap); } }
in Java
This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a character. Modify the given program to output a right triangle that instead uses the user-specified trianglechar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or* Each subsequent line will...
Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is the completed program. Pseudocode: DISPLAY Login information PROMPT for username/password output "Enter Username" input userName output "Enter password: " input password //If successful, display information pertaining to the specific user, as well as prompt for logout IF User type 'quit' Exit Program VALIDATE User Credentials IF NOT Validated Check number of Invalid Attempts IF user attempts equal three THEN DISPLAY error message...
Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...
This Python module will be menu driven and use conditions to check user input in order to determine which operation to perform based on the user’s menu selection. The output should look like the attached example output. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive) : 1. ‘CC’ – Character Count a. This operation will prompt the user for a string b. The number if characters will be counted...
This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...
Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...
IN JAVA Overview In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide the user to decide between different forms...
(Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....
14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...
Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...