Question

In: Computer Science

Okay, can someone please tell me what I am doing wrong?? I will show the code...

Okay, can someone please tell me what I am doing wrong??

I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance.

* * * * * HourlyTest Class * * * * *

import java.util.Scanner;
public class HourlyTest {   

public static void main(String[] args)     {
        String firstName;
        String lastName;
        double hoursWorked;
        double payRate;
      
        Scanner scan = new Scanner(System.in);
      
        // Ask user to enter values
        System.out.print("Enter first name: ");
        firstName = scan.nextLine();
      
        System.out.print("Enter last name: ");
        lastName = scan.nextLine();
      
        System.out.print("Enter the number of hours worked: ");
        hoursWorked = Double.parseDouble(scan.nextLine());
      
        System.out.print("Enter hourly rate: $");
        payRate = Double.parseDouble(scan.nextLine());

      
        // Create Hourly class object
        Hourly hourly = new Hourly();
    
        hourly.setFirstName(firstName);
        hourly.setLastName(lastName);     
        hourly.setHoursWorked(hoursWorked);
        hourly.setPayRate(payRate);       
      
        // Call method to get weeklyPay
        double weeklyPay = hourly.getPaycheck();
      
        // if enter uses invalid data
        while (weeklyPay == 0.0) {
            System.out.print("Enter the number of hours worked: ");
            hoursWorked = scan.nextInt();
          
            System.out.print("Enter hourly rate: $");
            payRate = scan.nextDouble();
            scan.nextLine();
          
            hourly.setHoursWorked(hoursWorked);
            hourly.setPayRate(payRate);
            weeklyPay = hourly.getPayRate();
        }
              
        // Print weekly weeklyPay value
        System.out.println("\n" + firstName + " " + lastName +
                "'s weekly paycheck will be: $" + String.format("%.2f", weeklyPay));
        scan.close();
      
   } // end of main method
  
} // end of class HourlyTest

* * * * * Hourly Class * * * * *

public class Hourly {
    // Setting private Instance variables
    private String firstName;
    private String lastName;
    private double hoursWorked;
    private double payRate;
    private double weeklyPay;
    private static final int MAXREGULAR = 40;
  
    // constructor
    public Hourly() {
        // set data field to zero
        this.firstName = "";
        this.lastName = "";
        this.hoursWorked = 0.0;
        this.payRate = 0.0;
    } // end of constructor
  
    // Set and get methods
    public String getFirstName() {
        return firstName;
    } // end of getter

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    } // end of setter

  
    public String getLastName() {
        return lastName;
    } // end of getter

    public void setLastName(String lastName) {
        this.lastName = lastName;
    } // end of setter   
  
    public double getHoursWorked() {
        return hoursWorked;
    } // end of getter

    public void setHoursWorked(double hoursWorked) {
        if (hoursWorked >= 1 && hoursWorked <= 80) {
            this.hoursWorked = hoursWorked;
        } else {
            System.out.println("\nYou must enter a number between 1 and 80. "
                    + "Please try again!\n");
            this.hoursWorked = 0.0;
        }      
    } // end of setter

    public double getPayRate() {
        return payRate;
    } // end of getter

    public void setPayRate(double payRate) {
        if (payRate >= 15 && payRate <= 30) {
            this.payRate = payRate;
        } else {
            System.out.println("\nYou must enter an hourly rate between "
                    + "$15 - $30. Please try again!\n");
            this.payRate = 0.0;
        }
    } // end of setter

    // Method to return weeklyPay
    public double getPaycheck() {
        this.weeklyPay = this.hoursWorked * this.payRate;
      
        if (hoursWorked > MAXREGULAR) { // if here was overtime. . .        
            double otHours = hoursWorked - MAXREGULAR; // calculate overtime hours
            double otPay = otHours * (payRate * 0.5); // pay "half" the rate for overtime hours worked
            weeklyPay = weeklyPay + otPay; // add overtime pay to regular pay
        }
        return weeklyPay;

    } // end of method getPaycheck
  
} //end of class Hourly

* * * * * What needs corrected based on the following comment by instructor * * * * *

For this assignment, you were supposed to create a constructor that that accepts those instance variables and performs some input validation and then prints an error message. Then, the HourlyTest class checks the weeklyPaycheck amount and, if it's 0, prompts the user for input again.

Please help!

Solutions

Expert Solution

       import java.util.Scanner;

public class HourlyTest {

    static class Hourly {
        // Setting private Instance variables
        private String firstName;
        private String lastName;
        private double hoursWorked;
        private double payRate;
        private double weeklyPay;
        private static final int MAXREGULAR = 40;
        Scanner scan = new Scanner(System.in);
      
        // constructor
        public Hourly() {
            // set data field to zero
            this.firstName = "";
            this.lastName = "";
            this.hoursWorked = 0.0;
            this.payRate = 0.0;
        } // end of constructor
      
        // Set and get methods
        public String getFirstName() {
            return firstName;
        } // end of getter

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        } // end of setter

      
        public String getLastName() {
            return lastName;
        } // end of getter

        public void setLastName(String lastName) {
            this.lastName = lastName;
        } // end of setter   
      
        public double getHoursWorked() {
            return hoursWorked;
        } // end of getter

        public void setHoursWorked(double hoursWorked) {
            
            while(hoursWorked < 1 || hoursWorked > 80) {
                System.out.println("\nYou must enter a number between 1 and 80. "
                        + "Please try again!\n");

                System.out.print("Enter the number of hours worked: ");
                hoursWorked = Double.parseDouble(scan.nextLine());
            }
            
            this.hoursWorked = hoursWorked;
        } // end of setter

        public double getPayRate() {
            return payRate;
        } // end of getter

        public void setPayRate(double payRate) {
            while(payRate < 15 || payRate > 30) {
                System.out.println("\nYou must enter an hourly rate between "
                        + "$15 - $30. Please try again!\n");
                System.out.print("Enter hourly rate: $");
                payRate = Double.parseDouble(scan.nextLine());
            }
            
            this.payRate = payRate;
        } // end of setter

        // Method to return weeklyPay
        public double getPaycheck() {
            this.weeklyPay = this.hoursWorked * this.payRate;
          
            if (hoursWorked > MAXREGULAR) { // if here was overtime. . .        
                double otHours = hoursWorked - MAXREGULAR; // calculate overtime hours
                double otPay = otHours * (payRate * 0.5); // pay "half" the rate for overtime hours worked
                weeklyPay = weeklyPay + otPay; // add overtime pay to regular pay
            }
            return weeklyPay;

        } // end of method getPaycheck
      
    } //end of class Hourly
    
    public static void main(String[] args) {
        String firstName;
        String lastName;
        double hoursWorked;
        double payRate;

        Scanner scan = new Scanner(System.in);

        // Create Hourly class object
        Hourly hourly = new Hourly();

        // Ask user to enter values
        System.out.print("Enter first name: ");
        firstName = scan.nextLine();
        hourly.setFirstName(firstName);

        System.out.print("Enter last name: ");
        lastName = scan.nextLine();
        hourly.setLastName(lastName);

        System.out.print("Enter the number of hours worked: ");
        hoursWorked = Double.parseDouble(scan.nextLine());
        hourly.setHoursWorked(hoursWorked);

        System.out.print("Enter hourly rate: $");
        payRate = Double.parseDouble(scan.nextLine());
        hourly.setPayRate(payRate);

        // Call method to get weeklyPay
        double weeklyPay = hourly.getPaycheck();

        // if enter uses invalid data
        while (weeklyPay == 0.0) {
            System.out.print("Enter the number of hours worked: ");
            hoursWorked = scan.nextInt();

            System.out.print("Enter hourly rate: $");
            payRate = scan.nextDouble();
            scan.nextLine();

            hourly.setHoursWorked(hoursWorked);
            hourly.setPayRate(payRate);
            weeklyPay = hourly.getPaycheck();
        }

        // Print weekly weeklyPay value
        System.out.println(
                "\n" + firstName + " " + lastName +
                "'s weekly paycheck will be: $" + String.format("%.2f", weeklyPay));
        scan.close();

    } // end of main method

} // end of class HourlyTest
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

can someone tell me if i am wrong on any of these???? THANKS In order to...
can someone tell me if i am wrong on any of these???? THANKS In order to be able to deliver an effective persuasive speech, you need to be able to detect fallacies in your own as well as others’ speeches. The following statements of reasoning are all examples of the following fallacies: Hasty generalization, mistaken cause, invalid analogy, red herring, Ad hominem, false dilemma, bandwagon or slippery slope. 1. __________bandwagon fallacy_______ I don’t see any reason to wear a helmet...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When the ball moves the paddles don't move and when the paddles move the ball doesn't move. from graphics import * import time, random def racket1_up(racket1):    racket1.move(0,20)        def racket1_down(racket1):    racket1.move(0,-20)   def racket2_up(racket2):    racket2.move(0,20)   def racket2_down(racket2):    racket2.move(0,-20)   def bounceInBox(shape, dx, dy, xLow, xHigh, yLow, yHigh):     delay = .005     for i in range(600):         shape.move(dx, dy)         center = shape.getCenter()         x = center.getX()         y = center.getY()         if x...
Can someone please tell me why I am getting errors. I declared the map and it's...
Can someone please tell me why I am getting errors. I declared the map and it's values like instructed but it's telling me I'm wrong. #include <iostream> #include <stdio.h> #include <time.h> #include <chrono> #include <string> #include <cctype> #include <set> #include <map> #include "d_state.h" using namespace std; int main() { string name; map<string,string> s; map<string,string>::iterator it; s[“Maryland”] = "Salisbury"; s[“Arizona”] = "Phoenix"; s[“Florida”] = "Orlando"; s[“Califonia”] = "Sacramento"; s[“Virginia”] = "Richmond"; cout << "Enter a state:" << endl; cin >> name;...
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter...
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter the name of the weather data file: temperatures.txt Tell me about your crew’s preferred temperature for sailing: What is the coldest temperature they can sail in? 60 What is the hottest temperature they can sail in? 80 Month 1: 93.3 percent of days in range. Month 2: 20.0 percent of days in range. Month 3: 58.1 percent of days in range. Month 4: 100.0...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
please show me or tell me a trick, on how can i tell right away when...
please show me or tell me a trick, on how can i tell right away when a characteristics equation(system) is 1)overdamped 2)underdamped 3)critically damped 4) nonlinear show each an example write neatly!!!!!
Can someone please tell me on how can I have a double and character as one...
Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks! This is what I got so far and the values I am getting are all integers. #pragma once #include <iostream> class Node { public:    int data;    Node* next;       // self-referential    Node()    {        data = 0;        next = nullptr;    }    Node(int value)    {        data...
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str,...
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str, index: int, guess: str)->str: if guess in phrase: current_view = current_view.replace(current_view[index], guess) else: current_view = current_view    return current_view update_char_view("animal", "a^imal" , 1, "n") 'animal' update_char_view("animal", "a^m^l", 3, "a") 'aamal'
Can you please check my answers and if I am wrong correct me. Thank you! A....
Can you please check my answers and if I am wrong correct me. Thank you! A. In today's interconnected world, many central banks communicate regularly and frequently with the public about the state of the economy, the economic outlook, and the likely future course of monetary policy. Communication about the likely future course of monetary policy is known as "forward guidance.". If the central bank increases the reserve ratio, as the market has perfectly expected, which of the following will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT