Question

In: Computer Science

I have created a method in java in my menu list and I have to put...

I have created a method in java in my menu list and I have to put all the work into the method but I cannot get it to print and am not sure where I am going wrong. this is my code:

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

String fName = "";

String lName = "";

double hoursWorked;

double hourlyRate;

int optionOne = 1;

int optionTwo = 2;

int optionThree = 3;

int optionFour = 4;

int choice;

System.out.println("Calculator Menu");

System.out.println("1) Wage Calculator");

System.out.println("2) Coupon Calculator");

System.out.println("3) Roman Number Converter");

System.out.println("4) Exit");

System.out.println("");

System.out.println("Enter choice: ");

choice = in.nextInt();

switch(choice) {

case 1:

System.out.println("Enter first name: ");

fName = in.next();

System.out.println("Enter last name: ");

lName = in.next();

System.out.println("Enter hourly rate: ");

hourlyRate = in.nextDouble();

System.out.println("Enter hours worked: ");

hoursWorked = in.nextDouble();

System.out.printf("Name: " + fName + "," + lName);

System.out.println("");

wageCalculator();

break;

case 2:

double purchaseAmount;

int purchaseType;

final double autoP = 1;

final double frag = 2;

final double acc = 3;

System.out.println("Enter purchase amount: ");

purchaseAmount = in.nextDouble(); {

System.out.println("Choose purchase type: ");

System.out.println("1) Auto Parts");

System.out.println("2) Fragrances");

System.out.println("3) Accessories");

purchaseType = in.nextInt();

} if (purchaseType == autoP) {

System.out.println("Your coupon is: $" + purchaseAmount*.10 + "(Auto Parts)");

} if (purchaseType == frag) {

System.out.println("Your coupon is: $" + purchaseAmount*.15 + "(Fragrances)");

} if (purchaseType == acc) {

System.out.println("Your coupon is: $" + purchaseAmount*.20 + "(Accessories)");

}

break;

case 3:

double calc_wages;

System.out.println("No option right now");

break;

case 4:

System.out.println("Thank you for using our program. Have a great day!");

}

// public static int convertRomanNumber(String str). {

// }

}

private static void wageCalculator() {

// TODO Auto-generated method stub

double hoursWorked = 0;

double hourlyRate = 0;

double overtimeHours;

double overtimePay = 0;

double regularPay = 0;

if (hoursWorked > 40) {

overtimeHours = hoursWorked - 40;

System.out.println("Overtime hours: " + overtimeHours);

overtimePay = overtimeHours*(1.5*hourlyRate);

System.out.println("Overtime Pay: " + overtimePay);

hoursWorked = 40;

}

regularPay = hourlyRate*hoursWorked;

System.out.println("Regular hours worked: " + hoursWorked);

System.out.println("Regular Pay: $" + regularPay);

System.out.printf("Total Pay $%.2f", regularPay + overtimePay);

}

}

Solutions

Expert Solution

Solution:

----------------------

You were getting wrong output because you didn't pass hourlyRate,hoursWorked value to wageCalculator() method. You have to passed these values explicitly as these values are used in diiferent function( main() and wageCalculator()). To get Proper Output use wageCalculator(hourlyRate,hoursWorked); inside case 1

And Declare method as

private static void wageCalculator(double hourlyRate,double hoursWorked) {
       // TODO Auto-generated method stub
       double overtimeHours;
       double overtimePay = 0;
       double regularPay = 0;
       if (hoursWorked > 40) {
       overtimeHours = hoursWorked - 40;
       System.out.println("Overtime hours: " + overtimeHours);
       overtimePay = overtimeHours*(1.5*hourlyRate);
       System.out.println("Overtime Pay: " + overtimePay);
       hoursWorked = 40;
       }
       regularPay = hourlyRate*hoursWorked;
       System.out.println("Regular hours worked: " + hoursWorked);
       System.out.println("Regular Pay: $" + regularPay);
       System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
      

       }

Full Code is Given Below:

=============================

import java.util.Scanner;

public class Menu {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner in = new Scanner(System.in);
       String fName = "";
       String lName = "";
       double hoursWorked;
       double hourlyRate;
       int optionOne = 1;
       int optionTwo = 2;
       int optionThree = 3;
       int optionFour = 4;
       int choice;
       System.out.println("Calculator Menu");
       System.out.println("1) Wage Calculator");
       System.out.println("2) Coupon Calculator");
       System.out.println("3) Roman Number Converter");
       System.out.println("4) Exit");
       System.out.println("");
       System.out.println("Enter choice: ");
       choice = in.nextInt();
       switch(choice) {
       case 1:
       System.out.println("Enter first name: ");
       fName = in.next();
       System.out.println("Enter last name: ");
       lName = in.next();
       System.out.println("Enter hourly rate: ");
       hourlyRate = in.nextDouble();
       System.out.println("Enter hours worked: ");
       hoursWorked = in.nextDouble();
       System.out.printf("Name: " + fName + "," + lName);
       System.out.println("");
       //passing values to function
       wageCalculator(hourlyRate,hoursWorked);
       break;
       case 2:
       double purchaseAmount;
       int purchaseType;
       final double autoP = 1;
       final double frag = 2;
       final double acc = 3;
       System.out.println("Enter purchase amount: ");
       purchaseAmount = in.nextDouble(); {
       System.out.println("Choose purchase type: ");
       System.out.println("1) Auto Parts");
       System.out.println("2) Fragrances");
       System.out.println("3) Accessories");
       purchaseType = in.nextInt();
       } if (purchaseType == autoP) {
       System.out.println("Your coupon is: $" + purchaseAmount*.10 + "(Auto Parts)");
       } if (purchaseType == frag) {
       System.out.println("Your coupon is: $" + purchaseAmount*.15 + "(Fragrances)");
       } if (purchaseType == acc) {
       System.out.println("Your coupon is: $" + purchaseAmount*.20 + "(Accessories)");
       }
       break;
       case 3:
       double calc_wages;
       System.out.println("No option right now");
       break;
       case 4:
       System.out.println("Thank you for using our program. Have a great day!");
       }
       // public static int convertRomanNumber(String str). {

       // }
       }
       private static void wageCalculator(double hourlyRate,double hoursWorked) {
       // TODO Auto-generated method stub
       double overtimeHours;
       double overtimePay = 0;
       double regularPay = 0;
       if (hoursWorked > 40) {
       overtimeHours = hoursWorked - 40;
       System.out.println("Overtime hours: " + overtimeHours);
       overtimePay = overtimeHours*(1.5*hourlyRate);
       System.out.println("Overtime Pay: " + overtimePay);
       hoursWorked = 40;
       }
       regularPay = hourlyRate*hoursWorked;
       System.out.println("Regular hours worked: " + hoursWorked);
       System.out.println("Regular Pay: $" + regularPay);
       System.out.printf("Total Pay $%.2f", regularPay + overtimePay);
      

       }

}

Output:

================

Code Snapshot:

====================


Related Solutions

In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
This is my coded "multipath adventure" that I created. Now I have to modify it to...
This is my coded "multipath adventure" that I created. Now I have to modify it to include loops but I am really struggling to do so. Below are the instructions and my current code that needs to be modified. Thank you in advance! You must have at least 1 for loop in your code The for loop must actually be used as a loop, and it must have real effect in your program (it is theoretically possible to use the...
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
Write a menu driven Java program which uses a method for each of the following operations:...
Write a menu driven Java program which uses a method for each of the following operations: (Note : The user should be allowed to repeat the operations as long as he wants to. Use appropriate number of parameters and return type for each method.) A. to find the sum of the following series (up to N terms). The program should    display the terms:              22 + 42 + 62… For example, if N=4, then the program should display the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT