Results 1 to 15 of 15
  1. #1
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Whats the problem. Can you give us clues to what kind of trouble your having.
    My IP address is 127.0.0.1

  3. #3
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    ----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.

  4. #4
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    ----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.

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  6. #6
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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;
    
    	 }
    You also never declared any of those variables.
    My IP address is 127.0.0.1

  8. #8
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Don't give up on me.

    Java 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;
    
    	 }
    InternetCharges.java:54: char cannot be dereferenced
    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

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    instead of char use string and instead of 'A' use "A".
    My IP address is 127.0.0.1

  10. #10
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Zosden View Post
    instead of char use string and instead of 'A' use "A".
    Just to be clear, everywhere where I have "char pkg" I should replace with "string pkg"
    and then make the changes to a that you mentioned?

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    yeah a b and c.
    My IP address is 127.0.0.1

  12. #12
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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;
    
    	 }
    	 }

  13. #13
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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.

  14. #14
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Figured it out with all your help. Thank you.

  15. #15
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    mark this thread as solved then.
    My IP address is 127.0.0.1

Similar Threads

  1. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  2. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM
  3. Newbie reporting
    By nelsaez in forum Introductions
    Replies: 0
    Last Post: 11-05-2007, 06:39 PM
  4. Newbie in applet, Help me
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:14 AM
  5. newbie: the app is loaded but i can't see it
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-21-2007, 08:14 AM

Posting Permissions

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