Question

Java Switch Case Make From Pseudocode

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.


0 0
Add a comment Improve this question Transcribed image text
Answer #1
/*
 * 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);
         
    }
     
}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Java Switch Case Make From Pseudocode
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in Java This program will output a right triangle based on user specified height triangleHeight and...

    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...

  • Write a C program that takes two sets of characters entered by the user and merge...

    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 Java program reads an integer from a keyboard and prints it out with other comments....

    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...

    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...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (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...

    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...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java 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...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods...

    CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT