I posted earlier and have worked through my program - but I am getting errors no
ID: 3623373 • Letter: I
Question
I posted earlier and have worked through my program - but I am getting errors now and hope someone can help me.
Unlike the example listed on this site, my program requires the use of a constructor - therefore I have the InternetCharges.java where variables are defined and calculations are done, and the InternetChargesIO.java which is the actual input/output (GUI if you will).
_______________________________________________________________________________
InternetCharges.java:
-------------------------
/**
* InternetCharges Class
* This class sets up variables for the InternetCharges class
*/
public class InternetCharges
{
private double hours; //Internet Hours Used
private char pkg; //Internet Package Selected
double charges; //Charges for Package + Hours Used
/**
* The constructor initializes the hours and pkg
* fields with the values passed to p and h.
* The charges field is assigned 0.
*/
public InternetCharges(char p,
double h)
{
pkg = p;
hours = h;
charges = 0.0;
}
/**
* The setPkg method accepts an argument
* that is stored in the pkg field.
*/
public void setPkg(char p)
{
pkg = p;
}
/**
* The setHours method accepts an argument
* that is stored in the hours field.
*/
public void setHours(double h)
{
hours = h;
}
/**
* The getPkg method returns the value
* that is stored in the pkg field.
*/
public char getPkg()
{
return pkg;
}
/**
* The getHours method returns the value
* that is stored in the hours field.
*/
public double getHours()
{
return hours;
}
/**
* The getCharges method determines and returns
* the charges for internet services.
*/
public double getCharges()
{
double charges;
switch(pkg)
{
case 'A':
if (hours<40)
charges=49.95;
else
charges=49.95+((hours-40)*2);
break;
case 'B':
if (hours<80)
charges=69.95;
else
charges=69.95+((hours-80)*1);
break;
case 'C':
charges=89.95;
break;
default:
charges=0;
break;
}
return charges;
}
}
___________________________________________________________________________
InternetChargesIO:
----------------------
import java.io.*; // Used for Input and Output
import java.util.Scanner; // Needed for the Scanner class
/**
* This program demonstrates the InternetCharges class.
*/
public class InternetChargesIO
{
public static void main(String[] args)
{
InternetCharges chargeType1; // To reference a InternetCharge object
String input; // To hold keyboard input
char p; // The package
double h; // The hours
double charges; // The charges for service
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Create a InternetCharges object.
chargeType1 = new InternetCharges(p, h);
//Create Welcome message with package menu.
System.out.println("Welcome to Satellite Internet Connect"
+ " Packages: "
+ " A: 1.0 Mbps $49.95, 40 hrs/month, $2 /hr "
+ " over 40 hrs ");
System.out.println(" B: 4.5 Mbps $69.95, 80 hrs/month, $1 /hr "
+ " over 80 hrs ");
System.out.println(" C: 12.0 Mbps, unlimited hours $79.95 ");
//Get and store the pkg and hours entered.
System.out.println("Enter the customer's package A, B, or C: ");
input = keyboard.nextLine();
p = input.charAt(0);
System.out.println("Enter the number of hours used: ");
h = keyboard.nextDouble();
//Determine the pkg and hours that were entered.
switch(p)
{
case 'a':
case 'A':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges);
break;
case 'b':
case 'B':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges);
break;
case 'c':
case 'C':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges);
break;
default:
System.out.println("Invalid package. Enter A, B, or C.");
}
System.exit();
}
}
___________________________________________________________________________
Compile Errors:
------------------
----jGRASP exec: javac -g InternetChargesIO.java
InternetChargesIO.java:48: cannot find symbol
symbol : variable getCharges
location: class InternetCharges
System.out.println("The charges are $" + chargeType1.getCharges);
^
InternetChargesIO.java:54: cannot find symbol
symbol : variable getCharges
location: class InternetCharges
System.out.println("The charges are $" + chargeType1.getCharges);
^
InternetChargesIO.java:60: cannot find symbol
symbol : variable getCharges
location: class InternetCharges
System.out.println("The charges are $" + chargeType1.getCharges);
^
InternetChargesIO.java:66: exit(int) in java.lang.System cannot be applied to ()
System.exit();
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
_______________________________________________________________________________
What am I doing wrong?
I really hope someone can review this and reply back to me asap with some comments and suggestions.
Thank you!!!
Explanation / Answer
please rate - thanks
I moved some things around and added a few cases and ()
I've tried to highligh all the changes, but may have missed some
import java.io.*; // Used for Input and Output
import java.util.Scanner; // Needed for the Scanner class
/**
* This program demonstrates the InternetCharges class.
*/
public class InternetChargesIO
{
public static void main(String[] args)
{
InternetCharges chargeType1; // To reference a InternetCharge object
String input; // To hold keyboard input
char p; // The package
double h; // The hours
double charges; // The charges for service
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Create Welcome message with package menu.
System.out.println("Welcome to Satellite Internet Connect"
+ " Packages: "
+ " A: 1.0 Mbps $49.95, 40 hrs/month, $2 /hr "
+ " over 40 hrs ");
System.out.println(" B: 4.5 Mbps $69.95, 80 hrs/month, $1 /hr "
+ " over 80 hrs ");
System.out.println(" C: 12.0 Mbps, unlimited hours $79.95 ");
//Get and store the pkg and hours entered.
System.out.println("Enter the customer's package A, B, or C: ");
input = keyboard.nextLine();
p = input.charAt(0);
System.out.println("Enter the number of hours used: ");
h = keyboard.nextDouble();
//Create a InternetCharges object.
chargeType1 = new InternetCharges(p, h);
//Determine the pkg and hours that were entered.
switch(p)
{
case 'a':
case 'A':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges());
break;
case 'b':
case 'B':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges());
break;
case 'c':
case 'C':
chargeType1.getPkg();
chargeType1.getHours();
System.out.println("The charges are $" + chargeType1.getCharges());
break;
default:
System.out.println("Invalid package. Enter A, B, or C.");
}
System.exit(0);
}
}
----------------------------------------------------------------
/**
* InternetCharges Class
* This class sets up variables for the InternetCharges class
*/
public class InternetCharges
{
private double hours; //Internet Hours Used
private char pkg; //Internet Package Selected
double charges; //Charges for Package + Hours Used
/**
* The constructor initializes the hours and pkg
* fields with the values passed to p and h.
* The charges field is assigned 0.
*/
public InternetCharges(char p,
double h)
{
pkg = p;
hours = h;
charges = 0.0;
}
/**
* The setPkg method accepts an argument
* that is stored in the pkg field.
*/
public void setPkg(char p)
{
pkg = p;
}
/**
* The setHours method accepts an argument
* that is stored in the hours field.
*/
public void setHours(double h)
{
hours = h;
}
/**
* The getPkg method returns the value
* that is stored in the pkg field.
*/
public char getPkg()
{
return pkg;
}
/**
* The getHours method returns the value
* that is stored in the hours field.
*/
public double getHours()
{
return hours;
}
/**
* The getCharges method determines and returns
* the charges for internet services.
*/
public double getCharges()
{
double charges;
switch(pkg)
{
case 'A':case 'a':
if (hours<40)
charges=49.95;
else
charges=49.95+((hours-40)*2);
break;
case 'B': case 'b':
if (hours<80)
charges=69.95;
else
charges=69.95+((hours-80)*1);
break;
case 'C': case 'c':
charges=89.95;
break;
default:
charges=0;
break;
}
return charges;
}
}