Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    Kidatheart96 is offline Member
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: I don't know what I did wrong!

    I found the code that was making the program wait. Here is what the command prompt looks like now:

    Please enter your name.
    jen
    Please enter Car type, Whether is luxury or antique car.
    luxury
    Please enter number of days would you like to rent.
    2
    Name:jen
    CarType:luxury
    NumberOfDays:2
    SubTotal:0.0
    Tax:0.0
    Total:0.0
    Press any key to continue . . .

    What do I have to do to get the math to output so I won't keep getting 0.0?

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: I don't know what I did wrong!

    Look at the code that is printing those 0s. Find the variable that has that 0 value and then look back through the code to see where that variable should be assigned a value before it is printed. Add code to assign the variable a value.

    The code is confusing to read because there are many variables with the same name as methods. The methods should have names that show what action they are taking. For example: computeTax() would be a better name than Tax()
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    Kidatheart96 is offline Member
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: I don't know what I did wrong!

    I inserted (compute) where it was needed. Here is what the new code looks like:

    Java Code:
    import java.util.Scanner;
    import java.math.BigDecimal;
    import java.util.*;
    
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays;
        private double Tax;
        private double Subtotal;
        private double Total;
        private double RentalRate;
    
        public void readInput()
        {
    	Scanner keyboard = new Scanner( System.in);
        System.out.println(" Please enter your name.");
        name= keyboard.nextLine();
        System.out.println(" Please enter Car type, Whether is luxury or antique car.");
        CarType= keyboard.nextLine();
        System.out.println(" Please enter number of days would you like to rent.");
        NumberOfDays= keyboard.nextInt();
        }
    
        public double computeSubtotal ()
        {
    	readInput();
        RentalRate=35;
        Subtotal= RentalRate * NumberOfDays;
        return Subtotal;}
    
        public double computeTax ()
        {
    	readInput();
    	Tax=0.09;
    	return Tax;}
    
        public double computeTotal ()
        {
    	readInput ();
        Tax = 0.09;
        Total= Subtotal * 0.09;
        return Total;}
    
        public void writeOutput() {
          System.out.println(" Name:" + name);
    	  System.out.println(" CarType:" + CarType);
    	  System.out.println(" NumberOfDays:" + NumberOfDays);
    	  System.out.println(" SubTotal:" + Subtotal);
    	  System.out.println("Tax:" + Tax);
          System.out.println(" Total:" + Total);}
    
        public static void main (String [] args) {
    	    {
    	    Bill myBill = new Bill();
            myBill.readInput();
            myBill.writeOutput(); }
    	}
    }
    How do I know where to assign the values to the variables? I don't see any zeros in the code.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: I don't know what I did wrong!

    The default values of primitive numeric variables is 0.
    If you want to see a different value, assign the variables a value when they are defined:
    Java Code:
        private double Tax = 44.4;
        private double Subtotal = 5.55;
        private double Total = 66.6;
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    Kidatheart96 is offline Member
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: I don't know what I did wrong!

    I changed the values at the beginning of the program. Now it looks like this:
    Java Code:
    import java.util.Scanner;
    import java.math.BigDecimal;
    import java.util.*;
    
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays;
        private double Tax=0.09;
        private double Subtotal=35 * NumberOfDays;
        private double Total=Subtotal * 0.09;
        private double RentalRate=35;
    The command prompt now looks like this:
    Please enter your name.
    jen
    Please enter Car type, Whether is luxury or antique car.
    luxury
    Please enter number of days would you like to rent.
    3
    Name:jen
    CarType:luxury
    NumberOfDays:3
    SubTotal:0.0
    Tax:0.09
    Total:0.0
    Press any key to continue . . .

    Now I can see the tax, so that's an improvement. Did I do something wrong to where I can't get the Subtotal and the Total?

  6. #26
    Kidatheart96 is offline Member
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: I don't know what I did wrong!

    Quote Originally Posted by Kidatheart96 View Post
    I changed the values at the beginning of the program. Now it looks like this:
    Java Code:
    import java.util.Scanner;
    import java.math.BigDecimal;
    import java.util.*;
    
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays;
        private double Tax=0.09;
        private double Subtotal=35 * NumberOfDays;
        private double Total=Subtotal * 0.09;
        private double RentalRate=35;
    The command prompt now looks like this:
    Please enter your name.
    jen
    Please enter Car type, Whether is luxury or antique car.
    luxury
    Please enter number of days would you like to rent.
    3
    Name:jen
    CarType:luxury
    NumberOfDays:3
    SubTotal:0.0
    Tax:0.09
    Total:0.0
    Press any key to continue . . .

    Now I can see the tax, so that's an improvement. Did I do something wrong to where I can't get the Subtotal and the Total?
    Okay, so I figured out how to arrange the numbers so that the output is correct, but it only happens when I input one particular number. Here is what the top of my program looks like:
    Java Code:
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays=1;
        private double Tax=0.09;
        private double Subtotal=NumberOfDays * 35;
        private double Total=Subtotal + 3.15;
        private double RentalRate=35;
    Here is what it looks like in the command prompt:

    Please enter your name.
    jen
    Please enter Car type, Whether is luxury or antique car.
    luxury
    Please enter number of days would you like to rent.
    1
    Name:jen
    CarType:luxury
    NumberOfDays:1
    SubTotal:35.0
    Tax:0.09
    Total:38.15
    Press any key to continue . . .

    When I input any other number into the number of days, the math does not add up. How can I make it so that the program will output the right math when the number of days are more than 1?

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: I don't know what I did wrong!

    How can I make it so that the program will output the right math when the number of days are more than 1?
    The code should use the numbers that the user enters, not the numbers that are initially assigned to those variables.
    You need to understand the order that the computer executes the statements in your program. It starts executing at the first statement in the main() method and continues in the order that the code specifies.

    Where are values assigned to the variables that you want to see printed out? If those variables get assigned values in a method, then the program must call those methods for there statements to be executed. The code currently calls the readInput() and writeOutput() methods. It does not call any of the compute... methods.

    As an exercise to see what order the statements are executed, go through the program and add a number as comment to each line in the program that shows what order that statement is executed in. I have labelled the first 5 statements. Continue numbering them to see when a statement is executed:
    Java Code:
    import java.util.Scanner;
    import java.math.BigDecimal;
    import java.util.*;
    
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays;
        private double Tax;
        private double Subtotal;
        private double Total;
        private double RentalRate;
    
        public void readInput()
        {
    	Scanner keyboard = new Scanner( System.in);                  // 3
        System.out.println(" Please enter your name.");             // 4
        name= keyboard.nextLine();                                  // 5
        System.out.println(" Please enter Car type, Whether is luxury or antique car.");
        CarType= keyboard.nextLine();
        System.out.println(" Please enter number of days would you like to rent.");
        NumberOfDays= keyboard.nextInt();
        }
    
        public double computeSubtotal ()
        {
    	readInput();
        RentalRate=35;
        Subtotal= RentalRate * NumberOfDays;
        return Subtotal;}
    
        public double computeTax ()
        {
    	readInput();
    	Tax=0.09;
    	return Tax;}
    
        public double computeTotal ()
        {
    	readInput ();
        Tax = 0.09;
        Total= Subtotal * 0.09;
        return Total;}
    
        public void writeOutput() {
          System.out.println(" Name:" + name);
    	  System.out.println(" CarType:" + CarType);
    	  System.out.println(" NumberOfDays:" + NumberOfDays);
    	  System.out.println(" SubTotal:" + Subtotal);
    	  System.out.println("Tax:" + Tax);
          System.out.println(" Total:" + Total);}
    
        public static void main (String [] args) {
    	    {
    	    Bill myBill = new Bill();                    // 1
            myBill.readInput();                         // 2
            myBill.writeOutput(); 
                }
    	}
    }
    Last edited by Norm; 12-14-2013 at 02:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Kidatheart96 is offline Member
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Wink Re: I don't know what I did wrong!

    Quote Originally Posted by Norm View Post
    The code should use the numbers that the user enters, not the numbers that are initially assigned to those variables.
    You need to understand the order that the computer executes the statements in your program. It starts executing at the first statement in the main() method and continues in the order that the code specifies.

    Where are values assigned to the variables that you want to see printed out? If those variables get assigned values in a method, then the program must call those methods for there statements to be executed. The code currently calls the readInput() and writeOutput() methods. It does not call any of the compute... methods.

    As an exercise to see what order the statements are executed, go through the program and add a number as comment to each line in the program that shows what order that statement is executed in. I have labelled the first 5 statements. Continue numbering them to see when a statement is executed:
    Java Code:
    import java.util.Scanner;
    import java.math.BigDecimal;
    import java.util.*;
    
    public class Bill
    {
    	private String name;
        private String CarType;
        private int NumberOfDays;
        private double Tax;
        private double Subtotal;
        private double Total;
        private double RentalRate;
    
        public void readInput()
        {
    	Scanner keyboard = new Scanner( System.in);                  // 3
        System.out.println(" Please enter your name.");             // 4
        name= keyboard.nextLine();                                  // 5
        System.out.println(" Please enter Car type, Whether is luxury or antique car.");
        CarType= keyboard.nextLine();
        System.out.println(" Please enter number of days would you like to rent.");
        NumberOfDays= keyboard.nextInt();
        }
    
        public double computeSubtotal ()
        {
    	readInput();
        RentalRate=35;
        Subtotal= RentalRate * NumberOfDays;
        return Subtotal;}
    
        public double computeTax ()
        {
    	readInput();
    	Tax=0.09;
    	return Tax;}
    
        public double computeTotal ()
        {
    	readInput ();
        Tax = 0.09;
        Total= Subtotal * 0.09;
        return Total;}
    
        public void writeOutput() {
          System.out.println(" Name:" + name);
    	  System.out.println(" CarType:" + CarType);
    	  System.out.println(" NumberOfDays:" + NumberOfDays);
    	  System.out.println(" SubTotal:" + Subtotal);
    	  System.out.println("Tax:" + Tax);
          System.out.println(" Total:" + Total);}
    
        public static void main (String [] args) {
    	    {
    	    Bill myBill = new Bill();                    // 1
            myBill.readInput();                         // 2
            myBill.writeOutput(); 
                }
    	}
    }
    I think I finally figured it out. Thanks for the help and all your time Norm. I appreciate it.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: I don't know what I did wrong!

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  3. What Am I doing Wrong?
    By siren1111 in forum New To Java
    Replies: 3
    Last Post: 12-20-2011, 02:01 AM
  4. Replies: 10
    Last Post: 07-16-2010, 12:34 AM
  5. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •