Results 1 to 15 of 15
Thread: Newbie with questions
- 06-04-2008, 08:35 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Newbie with questions-SOLVED
I'm 2 weeks in a basic Java class and I'm having all kinds of problems with an assignment. Here is the assignment(in red). Can someone help a newbie out?
Use this UML diagram to create the class
InternetCharges
-pkg:char
-hours:double
+InternetCharges(p:char, h:double)
+setPkg(p:char):void
+setHours(h:double):void
+getPkg():char
+getHours():double
+getChanges():double
XY_Prog4_11 Modify the data in the program so it matches these Sample Runs:
Welcome to Satellite Internet Connect
Packages:
A: 512Kbps $49.95, 40 hrs/month, $2/hr over 40 hrs
B: 1.0 Mbps $69.95 80 hrs/month, $1/hr over 80 hrs
C: 1.5Mbps unlimited hours
Enter the customer's package A, B, or C: A
Enter the number of hours used: 45
The charges are $59.95
Welcome to Satellite Internet Connect
Packages:
A: 512Kbps $49.95, 40 hrs/month, $2/hr over 40 hrs
B: 1.0 Mbps $69.95 80 hrs/month, $1/hr over 80 hrs
C: 1.5Mbps unlimited hours
Enter the customer's package A, B, or C:
Here is what I have so far
Java Code:public class InternetCharges { private char pkg; //Customers package private double hours; //Number of hours used /** *The constructor uses two parameters to accept *arguments: p and h. The value in p is assigned to *the pkg field and the value in h is assigned to *the hours field. The InternetCharges method is called */ public InternetCharges(char p, double h) { pkg=p; hours=h; } /** *The getPkg method returns the pkg field */ public char getPkg() { return pkg; } /** *The getHours method returns the hours field */ public double getHours() { return hours; } /** *The getCharges method returns the charges field. */ public double getCharges() { if (pkg = A) baseCharge = 49.95; addedCharge = (hours - 40)* 2.00; //2.00 per hour totalCharge = baseCharge + addedCharge; else if (pkg = B) baseCharge = 69.95; addedCharge = (hours - 80) * 1.00; //1.00 per hour totalCharge = baseCharge + addedCharge; else if (pkg = C) baseCharge = 79.95; totalCharge = baseCharge; return charges; } }
Java Code:import java.util.Scanner; //Scanner class /** *This program demo's internet charges */ public class IntChargesRun { public static void main(String[] args) { String input; char pkg; //Package A, B, or C double hours; //Number of hours used Scanner keyboard = new Scanner(System.in); //Scanner for keyboard input //Display instructions System.out.println("Welcome to Satellite Internet Connect Packages:" + '\n' + "A: 512Kbps $49.95, 40 hrs/month, $2/hr over 40 hrs" + '\n' + "B: 1.0Mbps $69.95, 80 hrs/month, $1/hr over 80 hrs" + '\n' + "C: 1.5Mbps unlimited hours" + '\n'); //Customers package type System.out.println("Enter the customer's package A, B, or C: "); pkg = input.charAt(0); //Gets the first character //Number of hours used System.out.println("Enter the number of hours used: "); hours = keyboard.nextDouble(); //gets hours InternetCharges ichgs = new InternetCharges(pkg, hours); double c = ichgs.getCharges(); System.out.println (c); } }Last edited by buzzdsm; 06-05-2008 at 02:51 PM.
- 06-04-2008, 09:05 PM #2
Whats the problem. Can you give us clues to what kind of trouble your having.
My IP address is 127.0.0.1
- 06-04-2008, 09:06 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
----jGRASP exec: javac -g C:\Program Files\Java\jdk1.5.0_15\bin\InternetCharges.java
InternetCharges.java:55: 'else' without 'if'
else if (pkg = B)
^
InternetCharges.java:59: 'else' without 'if'
else if (pkg = C)
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 06-04-2008, 09:07 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
----jGRASP exec: javac -g C:\Program Files\Java\jdk1.5.0_15\bin\IntChargesRun.java
InternetCharges.java:55: 'else' without 'if'
else if (pkg = B)
^
InternetCharges.java:59: 'else' without 'if'
else if (pkg = C)
^
InternetCharges.java:51: cannot find symbol
symbol : variable A
location: class InternetCharges
if (pkg = A)
^
InternetCharges.java:51: incompatible types
found : char
required: boolean
if (pkg = A)
^
InternetCharges.java:52: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
baseCharge = 49.95;
^
InternetCharges.java:53: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
addedCharge = (hours - 40)* 2.00; //2.00 per hour
^
InternetCharges.java:54: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:54: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:54: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:57: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
addedCharge = (hours - 80) * 1.00; //1.00 per hour
^
InternetCharges.java:58: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:58: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:58: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:61: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge;
^
InternetCharges.java:61: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge;
^
InternetCharges.java:63: cannot find symbol
symbol : variable charges
location: class InternetCharges
return charges;
^
16 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 06-04-2008, 09:10 PM #5
Java Code:public double getCharges() { if (pkg = A) { baseCharge = 49.95; addedCharge = (hours - 40)* 2.00; //2.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg = B) { baseCharge = 69.95; addedCharge = (hours - 80) * 1.00; //1.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg = C) { baseCharge = 79.95; totalCharge = baseCharge; } return charges; }My IP address is 127.0.0.1
- 06-04-2008, 09:12 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
I tried this above and now get these errors.
----jGRASP exec: javac -g C:\Program Files\Java\jdk1.5.0_15\bin\InternetCharges.java
InternetCharges.java:51: cannot find symbol
symbol : variable A
location: class InternetCharges
if (pkg = A)
^
InternetCharges.java:51: incompatible types
found : char
required: boolean
if (pkg = A)
^
InternetCharges.java:53: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
baseCharge = 49.95;
^
InternetCharges.java:54: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
addedCharge = (hours - 40)* 2.00; //2.00 per hour
^
InternetCharges.java:55: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:55: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:55: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:57: cannot find symbol
symbol : variable B
location: class InternetCharges
else if (pkg = B)
^
InternetCharges.java:57: incompatible types
found : char
required: boolean
else if (pkg = B)
^
InternetCharges.java:59: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
baseCharge = 69.95;
^
InternetCharges.java:60: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
addedCharge = (hours - 80) * 1.00; //1.00 per hour
^
InternetCharges.java:61: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:61: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:61: cannot find symbol
symbol : variable addedCharge
location: class InternetCharges
totalCharge = baseCharge + addedCharge;
^
InternetCharges.java:63: cannot find symbol
symbol : variable C
location: class InternetCharges
else if (pkg = C)
^
InternetCharges.java:63: incompatible types
found : char
required: boolean
else if (pkg = C)
^
InternetCharges.java:65: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
baseCharge = 79.95;
^
InternetCharges.java:66: cannot find symbol
symbol : variable totalCharge
location: class InternetCharges
totalCharge = baseCharge;
^
InternetCharges.java:66: cannot find symbol
symbol : variable baseCharge
location: class InternetCharges
totalCharge = baseCharge;
^
InternetCharges.java:68: cannot find symbol
symbol : variable charges
location: class InternetCharges
return charges;
^
20 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 06-04-2008, 09:34 PM #7
You also never declared any of those variables.Java Code:public double getCharges() { if (pkg.equals('A')) { baseCharge = 49.95; addedCharge = (hours - 40)* 2.00; //2.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals('B')) { baseCharge = 69.95; addedCharge = (hours - 80) * 1.00; //1.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals('C')) { baseCharge = 79.95; totalCharge = baseCharge; } return charges; }My IP address is 127.0.0.1
- 06-04-2008, 09:39 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Don't give up on me.
InternetCharges.java:54: char cannot be dereferencedJava Code:public double getCharges() { double baseCharge; double addedCharge; double totalCharge; if (pkg.equals('A')) { baseCharge = 49.95; addedCharge = (hours - 40)* 2.00; //2.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals('B')) { baseCharge = 69.95; addedCharge = (hours - 80) * 1.00; //1.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals('C')) { baseCharge = 79.95; totalCharge = baseCharge; } return charges; }
if (pkg.equals('A'))
^
InternetCharges.java:60: char cannot be dereferenced
else if (pkg.equals('B'))
^
InternetCharges.java:66: char cannot be dereferenced
else if (pkg.equals('C'))
^
InternetCharges.java:71: cannot find symbol
symbol : variable charges
location: class InternetCharges
return charges;
^
4 errors
- 06-04-2008, 10:19 PM #9
instead of char use string and instead of 'A' use "A".
My IP address is 127.0.0.1
- 06-04-2008, 10:23 PM #10
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
- 06-04-2008, 11:28 PM #11
yeah a b and c.
My IP address is 127.0.0.1
- 06-05-2008, 01:40 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Thanks a ton for all your help. I'm now down to one error. Here is the error and the code.
InternetCharges.java:72: variable Charges might not have been initialized
return Charges;
^
1 error
Java Code:public class InternetCharges { private String pkg; //Customers package private double hours; //Number of hours used /** *The constructor uses two parameters to accept *arguments: p and h. The value in p is assigned to *the pkg field and the value in h is assigned to *the hours field. The InternetCharges method is called */ public InternetCharges(String p, double h) { pkg=p; hours=h; } /** *The getPkg method returns the pkg field */ public String getPkg() { return pkg; } /** *The getHours method returns the hours field */ public double getHours() { return hours; } /** *The getCharges method returns thestringges field. */ public double getCharges() { double baseCharge; double addedCharge; double totalCharge; double Charges; if (pkg.equals("A")) { baseCharge = 49.95; addedCharge = (hours - 40)* 2.00; //2.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals("B")) { baseCharge = 69.95; addedCharge = (hours - 80) * 1.00; //1.00 per hour totalCharge = baseCharge + addedCharge; } else if (pkg.equals("C")) { baseCharge = 79.95; totalCharge = baseCharge; } return Charges; } }
- 06-05-2008, 01:54 PM #13
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
Basically the value charges that you declare in getCharges is not initalised, as the error states.
As you only set this value in a conditional statement (if) there is a chance that if none of those conditions is met then your variable charges will not actually point to a value.
I don't know what is best for your program, you could assign a value like -1 to charges and test for this value after your if statements have finished and throw an exception if the value is this.
- 06-05-2008, 02:54 PM #14
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Figured it out with all your help. Thank you.
- 06-05-2008, 04:11 PM #15
Similar Threads
-
newbie needs help...
By vicky08 in forum New To JavaReplies: 2Last Post: 03-31-2008, 04:26 PM -
Newbie
By CSnoob87 in forum IntroductionsReplies: 2Last Post: 02-18-2008, 08:49 AM -
Newbie reporting
By nelsaez in forum IntroductionsReplies: 0Last Post: 11-05-2007, 06:39 PM -
Newbie in applet, Help me
By barney in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:14 AM -
newbie: the app is loaded but i can't see it
By tamayo in forum New To JavaReplies: 1Last Post: 07-21-2007, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks