-
Newly Confuse to Java Programs
A friend gave me this try to figure out, it's taken from a psedue code in which you must go from it.
The form at the bottom of page.
Any hlp is apprecieated.
Code:
import java.util.Scanner; // program uses class Scanner
public class Garage
{
// Instantiate a local variable named input of Scanner class
Scanner input = new Scanner( System.in );
int customerNumber = 1;
double totalParkingReceipts = 0.0;
double parkingCharge;
double hoursParked;
//String variable hours parked prompt
String hoursParkedPrompt = "Enter number of hours parked for customer %2d, or <ctrl>z to stop: ";
//String variable named programOutput to Summary
String programOutput = ("\nSummary of Today's Receipts:\n");
//Concatenate line 1 of output column
String line1 = programOutput += String.format ("\n\t%8s %6s %7s","Customer","Hours ","Parking");
//Concatenate line 2 of output column
String line2 = programOutput += String.format ("\n\t%8s %6s %7s"," Numbers ","Parked"," Fee ");
//Display Headline
System.out.println("\nParking Attendant Activity:\n");
//Display prompt
System.out.printf("\t "+hoursParkedPrompt, customerNumber);
while (input.hasNext());
{
//Assign input.nextDouble to hoursParked
hoursParked = input.nextDouble();
//add parkingCharge TO totalParkingReceipts
totalParkingReceipts = parkingCharge;
//Concatenate formatted String to programOutput
String.format(programOutput = "\n\t%8d %6.1f $%6.2f", customerNumber, hoursParked, parkingCharge);
//Display prompt
System.out.printf("\tCharge for customer %2d is $%6.2f; " + hoursParkedPrompt,
customerNumber, parkingCharge, ++customerNumber);
}// end while
//Concatenate formatted String to programOutput
String.format(programOutput = "\n\t%-17s $%6.2f"," Total Receipts",totalParkingReceipts);
//Display programOutput
System.out.println programOutput;
//Calculate charge method of class Garage
private static Object calculateCharges(double hoursParked)
{
//Define named variable constant's
final double minimumParkingCharge = 2.0;
final double maximumParkingCharge = 10.0;
final double maximumParkingHoursForMinimumParkingCharge = 3.0;
final double chargePerHourAfterMinimum = 0.5;
//Assign minimumParkingCharge to parkingCharge
double parkingCharge = minimumParkingCharge;
if (hoursParked > maximumParkingHoursForMinimumParkingCharge);
parkingCharge = minimumParkingCharge + chargePerHourAfterMinimum +
Math.ceil(hoursParked - maximumParkingHoursForMinimumParkingCharge);
// end if
if (parkingCharge > maximumParkingCharge);
parkingCharge = maximumParkingCharge;
//end if
return parkingCharge;
}//end method
}
errors I get as follow:
-Called Compiler C:\PROGRA~1\Java\JDK15~1.0_0\bin\javac.exe-
-Target File: D:\CSIS12~1\JUMPDR~1\6\Garage.java-
D:\CSIS12~1\JUMPDR~1\6\Garage.java:28: <identifier> expected
System.out.println("\nParking Attendant Activity:\n");
^
D:\CSIS12~1\JUMPDR~1\6\Garage.java:31: <identifier> expected
System.out.printf("\t "+hoursParkedPrompt, customerNumber);
^
D:\CSIS12~1\JUMPDR~1\6\Garage.java:33: illegal start of type
while (input.hasNext());
^
D:\CSIS12~1\JUMPDR~1\6\Garage.java:33: <identifier> expected
while (input.hasNext());
^
D:\CSIS12~1\JUMPDR~1\6\Garage.java:52: <identifier> expected
String.format(programOutput = "\n\t%-17s $%6.2f"," Total Receipts",totalParkingReceipts);
^
5 errors
-Finished-
Exhibit A-1: UML Class Diagram of Class Garage
Garage
+openForBusiness()
-calculateCharge(hoursParked : double): double
Exhibit A-2: Pseudo code for openForBusiness Method of Class Garage
This public method has no return value, and has no parameters:
A-2-01) INSTANTIATE a local variable named input of the Scanner class
A-2-02) Define a local int variable named customerNumber and initialize it to 1
A-2-03) Define a local double variable named totalParkingReceipts and initialize it to 0.0
A-2-04) Define a local double variable named parkingCharge
A-2-05) Define a local double variable named hoursParked
A-2-06) Define a local String variable named hoursParkedPrompt and initialize it to
“Enter number of hours parked for customer %2d, or <ctrl>z to stop:Δ “
A-2-07) Define a local String variable named programOutput and initialize it to “\nSummary of Today’s Receipts:\n”
A-2-08) CONCATENATE line one of output column headings onto programOutput using method format of the String class:
programOutput += String.format(“\n\t%8sΔΔΔ%6sΔΔΔ%7s”,”Customer”,”HoursΔ”,”Parking”);
A-2-09) CONCATENATE line two of output column headings onto programOutput using method format of the String class:
programOutput += String.format(“\n\t%8sΔΔΔ%6sΔΔΔ%7s”,”ΔNumberΔ”,”Parked”,”ΔΔFeeΔΔ”);
A-2-10) DISPLAY the task / programmer identification line followed by a blank line
A-2-11) DISPLAY the “Parking Attendant Activity” heading followed by a blank line
A-2-12) Using method printf, DISPLAY the following prompt
(“\tΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ” + hoursParkedPrompt, customerNumber)
A-2-13) WHILE (input.hasNext())
A-2-14) ASSIGN input.nextDouble() TO hoursParked
A-2-15) ASSIGN calculateCharge (hoursParked) TO parkingCharge
A-2-16) ADD parkingCharge TO totalParkingReceipts
A-2-17) CONCATENATE the following formatted string onto programOutput:
(“\n\t%8dΔΔΔ%6.1fΔΔΔ$%6.2f”, customerNumber, hoursParked, parkingCharge)
A-2-18) Using method printf, DISPLAY the following prompt:
(“\tCharge for customer %2d is $%6.2f;Δ” + hoursParkedPrompt,
customerNumber, parkingCharge, ++customerNumber)
A-2-19) END WHILE
A-2-20) CONCATENATE the following formatted string onto programOutput:
(\n\t%-17sΔΔΔ$%6.2f”, “ΔΔΔTotal Receipts”, totalParkingReceipts)
A-2-21) Using method println, DISPLAY programOutput
A-2-22) Using method println, DISPLAY a blank line followed by the “End of program” line
Exhibit A-3: Pseudo code for calculateCharge Method of Class Garage
This private method has a double return value, and has one parameter: hoursParked of type double:
A-3-01) Define a named constant for minimumParkingCharge with a value of 2.0;
A-3-02) Define a named constant for maximumParkingCharge with a value of 10.0;
A-3-03) Define a named constant for maximumParkingHoursForMinimumParkingCharge with a value of 3.0;
A-3-04) Define a named constant for chargePerHourAfterMinumum with a value of 0.5;
A-3-05) Define a local double variable named parkingCharge;
A-3-06) ASSIGN minimumParkingCharge TO parkingCharge
A-3-07) IF hoursParked > maximumParkingHoursForMinimumParkingCharge THEN
A-3-08) ASSIGN the expression ( minimumParkingCharge plus product of chargePerHourAfterMinumum and
Math.ceil(hoursParked minus maximumParkingHoursForMinimumParkingCharge))
TO parkingCharge
A-3-09) END-IF
A-3-10) IF parkingCharge IS GREATHER THAN maximumParkingCharge THEN
A-3-11) ASSIGN maximumParkingCharge TO parkingCharge
A-3-12) END IF
A-3-13) RETURN parkingCharge
-
You are writing code outside of a function. That doesn't make sense to the compiler. You should add a main method.
Code:
<<imports>>
class Garage
{
public static void main(String[] args) {
<<Write your code here>>
}
<<Other methods>>
}
That will solve most of your problems. Again, there are some more errors, but they are easy to solve.
Similar Threads
-
Replies: 9
Last Post: 09-19-2007, 05:58 AM
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks