Question

In: Computer Science

Write one Java program and satisfy the following requirements: Rewrite the following if -else if -...

Write one Java program and satisfy the following requirements:

  1. Rewrite the following if -else if - else statement as an equivalent switch statement.  

if (letter == 'A' | | letter == 'a')

        System.out.println("Excellent");

        else if (letter == 'B' | | letter == 'b')

               System.out.println("You can do better");

        else if (letter == 'C' | | letter == 'c')

               System.out.println("Try harder");

        else if (letter == 'D' | | letter == 'd')

               System.out.println("Try much harder");

        else

               System.out.println("Try another major! ");

  1. Write a code to read three different integers and find the larger value by using       if ... else if … else statement.

3. Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay. Select the selection statement (if, if-else, or switch) that makes the most sense.

     If pay is more than 100,000, tax rate is 40%

     If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%

     If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%

     If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%

     If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%

     If pay is less than or equal to 5,000, tax rate is 0%

Solutions

Expert Solution

ANSWER:-

QUESTION (1):-

switch(letter)
       {
           case 'A':
       case 'a':
           System.out.println("Excellent");
           break;
       case 'B':
       case 'b':
           System.out.println("You can do better");
           break;
       case 'C':
       case 'c':
           System.out.println("Try harder");
           break;
       case 'D':
       case 'd':
           System.out.println("Try much harder");
           break;
       default:
       System.out.println("Try another major! ");
       }

QUESTION (2):-

import java.util.Scanner;

class Largest
{
public static void main(String args[])
{
int x, y, z,max=0;
System.out.println("Enter three integers : ");
Scanner in = new Scanner(System.in);

x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
max=x;
if (x > y && x > z)
max=x;
else if (y > x && y > z)
max=y;
else if (z > x && z > y)
max=z;
System.out.println("Largest number : "+max);
}
}

OUTPUT:-

QUESTION (3):-

import java.util.Scanner;

class TaxRate
{
public static void main(String args[])
{
double taxRate=0,pay;
System.out.println("Enter pay : ");
Scanner in = new Scanner(System.in);
pay=in.nextDouble();
if (pay>100000)
taxRate=40;
else if (pay>60000 && pay<=100000)
taxRate=30;
else if (pay>30000 && pay<=60000)
taxRate=20;
else if (pay>15000 && pay<=30000)
taxRate=10;
else if (pay>5000 && pay<=15000)
taxRate=5;
else if (pay<=5000)
taxRate=0;
}
}


Related Solutions

Write one Java program and satisfy the following requirements: Write a method called cube that accepts...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts one integer parameter and returns that value raised to the third power. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method) Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method) Overload findMax that accepts one...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT