|
-
Jun 4th, 2008, 01:01 PM
#1
Thread Starter
New Member
Basic Java Question
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
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);
}
}
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;
}
}
-
Jun 5th, 2008, 07:44 PM
#2
Re: Basic Java Question
This is a basics class so check the basics:
Did you declare your variables properly before using them?
What's the difference between 'A' and just A in the source code?
What is a programming block? How to start and end a block? How are If..Else blocks constructed?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|