d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help A Beginner With Java > Very Firt Program
Add Reply New Topic New Poll
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 5 2013 08:52am
the actually assignment is in the comments however im trying to learn it, im just getting some errors that i cant figure out how to get around.

Code
//Covering Chapter 2, Basic Elements of Java.
// 10 points
//Due 6pm, 9/5
//
//You found an exciting summer job for five weeks. It pays $15.50 per hour.
//Suppose that the total tax you pay on your summer job income is 14%. After paying
// the taxes, you spend 10% of your net income to buy new clothes and other accessories
// for the next school year and 1% to buy school supplies. After buying clothes and school
//   supplies, you use 25% of the remaining money to buy savings bonds. For each dollar you
//   spend to buy savings bonds, your parents spend $0.50 to buy additional savings bonds for
//you. Write a program that prompts the user to enter the pay rate for an hour and the
// number ofhours you worked each week. The program then outputs the following:
//a. Your income before and after taxes from your summer job
//b. The money you spend on clothes and other accessories
//c. The money you spend on school supplies
//d. The money you spend to buy savings bonds
//e. The money your parents spend to buy additional savings bonds for you
//
//Be sure to write a description at the top of your program similar to the one we saw in the slides.
//Also be sure to write comments every few lines and use indentation that is consistent with the examples we have seen. 10 points


public class InterestCh2
{
 static Scanner console = new Scanner(System.in);
  public static void main(String[] args)  
 
  {
     final double PAY_RATE = 15.50;
     final double TAXES = .14;
     final double CLOTHES = .10;
     final double SCHOOL_SUPPLIES = .01;
     final double REMAINDER_SAVINGS_BONDS = .25;
     final double PARENTS_BONDS = .50;
     int HOURS;
     
     System.out.println("Enter number of hours worked");
     
     HOURS = console.nextInt();
 
 
  }
 
 
 
}


The Error

Code
InterestCh2.java:25: error: cannot find symbol
 static Scanner console = new Scanner(System.in);
        ^
 symbol:   class Scanner
 location: class InterestCh2
InterestCh2.java:25: error: cannot find symbol
 static Scanner console = new Scanner(System.in);
                              ^
 symbol:   class Scanner
 location: class InterestCh2
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


can anyone here help me with this?


e- wow... just figured it out, completely omited java.util*;


This post was edited by lilwillis121 on Sep 5 2013 08:56am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 5 2013 09:06am
What IDE are you using (if any)?. If you're using Eclipse, you can fix all import errors by pressing ctrl + shift + o
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 5 2013 09:15am
we were assigned to use jgrasp

Just going through the process and im getting

InterestCh2.java:36: error: variable Hours might not have been initialized

but Hour is dependent on imput. help?

Code
import java.util.*;

public class InterestCh2
{
 static Scanner console = new Scanner(System.in);
  public static void main(String[] args)  
 
  {
     final double PAY_RATE = 15.50;
     final double TAXES = .86;
     final double CLOTHES = .10;
     final double SCHOOL_SUPPLIES = .01;
     final double REMAINDER_SAVINGS_BONDS = .25;
     final double PARENTS_BONDS = .50;
     int Hours;
     final double MONEY = (Hours * PAY_RATE);
     
     System.out.println("Enter number of hours worked");
     
     Hours = console.nextInt();
     
     System.out.println("Total Pay = " + Hours * PAY_RATE );
     System.out.println("Money before taxes:" + Hours * PAY_RATE);
     System.out.println("Money after taxes:" + MONEY * TAXES);




Lol... chugging along here... figured tat one out.

This post was edited by lilwillis121 on Sep 5 2013 09:43am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 5 2013 09:43am
Quote (lilwillis121 @ Sep 5 2013 11:15am)
we were assigned to use jgrasp

Just going through the process and im getting

InterestCh2.java:36: error: variable Hours might not have been initialized

but Hour is dependent on imput. help?

Code
import java.util.*;

public class InterestCh2
{
 static Scanner console = new Scanner(System.in);
  public static void main(String[] args)  
 
  {
     final double PAY_RATE = 15.50;
     final double TAXES = .86;
     final double CLOTHES = .10;
     final double SCHOOL_SUPPLIES = .01;
     final double REMAINDER_SAVINGS_BONDS = .25;
     final double PARENTS_BONDS = .50;
     int Hours;
     final double MONEY = (Hours * PAY_RATE);
     
     System.out.println("Enter number of hours worked");
     
     Hours = console.nextInt();
     
     System.out.println("Total Pay = " + Hours * PAY_RATE );
     System.out.println("Money before taxes:" + Hours * PAY_RATE);
     System.out.println("Money after taxes:" + MONEY * TAXES);



change
Code
int Hours;
to
Code
int Hours = 0;


or remove that line and change line
Code
Hours = console.nextInt();
to
Code
int Hours = console.nextInt();



Also, you should change the variable name to "hours" instead of "Hours". It's proper syntax to start variable names with a lowercase letter

Also, move
Code
final double MONEY = (Hours * PAY_RATE);
after you initialize Hours

This post was edited by labatymo on Sep 5 2013 09:47am
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 5 2013 09:46am
Quote (labatymo @ Sep 5 2013 10:43am)
change int Hours; to int Hours = 0;


also, you should change the variable name to "hours" instead of "Hours". It's proper syntax to start variable names with a lowercase letter


question, is it cheating to keep declaring variables as i go down to avoid having huge arithmetic lines? for example

Code

     System.out.println("Enter number of hours worked");
     
     int Hours = console.nextInt();
           
     System.out.println("Total Pay = " + Hours * PAY_RATE );
     
     System.out.println("Money before taxes:" + Hours * PAY_RATE);
     
     final double MONEY = (Hours * PAY_RATE);
     
     System.out.println("Money after taxes:" + MONEY * TAXES);
     
     final double AFTER_TAX = ( MONEY * TAXES);
     
     System.out.println("Money that I spent on clothes and other accessories: " + AFTER_TAX * CLOTHES);
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 5 2013 09:54am
No, that's good, because you're using the variables more than once. If you were to declare a variable and only use it once, that's bad syntax.
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 5 2013 09:56am
Quote (labatymo @ Sep 5 2013 10:54am)
No, that's good, because you're using the variables more than once. If you were to declare a variable and only use it once, that's bad syntax.


awesome. Once i figured out that its been simi-smooth sailing. just mental head games. i still got about 8 hours, and im almost done so im doing well so far haha.

This post was edited by lilwillis121 on Sep 5 2013 09:56am
Member
Posts: 19,377
Joined: Jul 10 2007
Gold: 4,678.60
Sep 5 2013 10:12am
Ok, im not getting any errors, however, the math isnt checking out.

Code
import java.util.*;

public class InterestCh2
{
 static Scanner console = new Scanner(System.in);
  public static void main(String[] args)  
 
  {
     final double PAY_RATE = 15.50;
     final double TAXES = .86;
     final double CLOTHES = .10;
     final double SCHOOL_SUPPLIES = .01;
     final double REMAINDER_SAVINGS_BONDS = .25;
     final double PARENTS_BONDS = .50;
     
     System.out.println("Enter number of hours worked");
     
     int hours = console.nextInt();
           
     System.out.println("Total Pay = " + hours * PAY_RATE );
     
     System.out.println("Money before taxes:" + hours * PAY_RATE);
     
     final double MONEY = (hours * PAY_RATE);
     
     System.out.println("Money after taxes:" + MONEY * TAXES);
     
     final double AFTER_TAX = ( MONEY * TAXES);
     
     System.out.println("Money that I spent on clothes and other accessories: " + AFTER_TAX * CLOTHES);
     
     System.out.println("Money I spent on school supplies " + AFTER_TAX * SCHOOL_SUPPLIES);
     
     final double BEFORE_BONDS = (AFTER_TAX - (SCHOOL_SUPPLIES + CLOTHES) );
     
     System.out.println("Money I spent on Bonds " + BEFORE_BONDS);
     
     
    }
 
 
 
}


Code
----jGRASP exec: java InterestCh2

Enter number of hours worked
10
Total Pay = 155.0
Money before taxes:155.0
Money after taxes:133.3
Money that I spent on clothes and other accessories: 13.330000000000002
Money I spent on school supplies 1.3330000000000002
Money I spent on Bonds 133.19



its works well until bonds should be 133.3 - clothes -school supplies = 119.somthing.
i dont think the "-" sign is working correcly

This post was edited by lilwillis121 on Sep 5 2013 10:14am
Retired Moderator
Posts: 18,570
Joined: Feb 27 2004
Gold: 11,858.00
Trader: Trusted
Sep 5 2013 11:02am
Your math is off which is the problem.

All of your calculations are correct up to and including the money spent on clothing.

However, to get the money on the school supplies you need to figure out how much money you have left over after buying the clothes, and take 1% of that total. Not 1% of what you spent on the clothing.

Then to get the bonds, you need to figure out what the total amount of money you have after you got the supplies and take 25% of that total.

The numbers I got were $13.33 on clothes, $1.20 on supplies, and $29.69 on bonds.

I'll let you figure the rest out.

**Edit - Also, you may want to re-read the instructions. The instructor says they want you to allow the user to input not only the number of hours worked, but also the pay rate. You currently only allow the user to input the hours worked and you have the pay rate declared as a final.

This post was edited by Blind[zF] on Sep 5 2013 11:10am
Member
Posts: 1,534
Joined: Jan 15 2008
Gold: 1,383.50
Sep 9 2013 12:11am
It's good practise to use BigDecimal when dealing with currency. Float and double are big no-no's.

Use it's string constructor, like this:
BigDecimal bc = new BigDecimal("10364055.81")
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll