- #1
ProPatto16
- 326
- 0
dfjchjd
Last edited:
It would be better if you code post the code. Use the code tags when you post and it will stay indented.ProPatto16 said:Not sure why it's centred that thread, hope it's still readable
// Parking.java
// Class to calculate parking fee
import java.util.Scanner; // import Scanner
public class Parking
{
int charge; // charge for parking
int receipt; // receipt total of all customers
int customer; // number of customers
int entryTime; // user inputs entry time
int exitTime; // user inputs exit time
int timeDiffMin; // time difference in minutes
int timeDifference; // time difference
public void beginCharging()
{
Scanner input = new Scanner ( System.in ); // create new Scanner
customer = 1; // initialize customer count
System.out.println( "\nHello and welcome!" );
System.out.println( "This is a parking fee program." );
while ( customer <= 9)
{
System.out.printf(
"Please enter entry and exit time for customer %d\n",
customer ); // prompt
System.out.print( "Entry Time: " ); // input entry time
entryTime = input.nextInt();
System.out.print( "Exit time: " ); // input exit time
exitTime = input.nextInt();
[B][COLOR="Blue"]// This is all that you need.
calculateCharges();[/COLOR][/B]
System.out.printf( "Thank you, your charge is $%d\n\n",
charge ); // display charge
customer = customer + 1;
receipt = receipt + charge;
} // end while statement
System.out.println( "Thank you for using this program" );
System.out.printf( "Total of all charges: $%d", receipt );
System.out.println( "Have a nice day!" );
} // end method beginCharging
public void calculateCharges()
{
timeDiffMin = (((exitTime/100)*60)+(exitTime%100))
-(((entryTime/100)*60)+(entryTime%100));
timeDifference = ((timeDiffMin/60)*100)+(timeDiffMin%60);
if ( timeDifference > 800 )
charge = 20;
else if ( timeDifference > 700 )
charge = 18;
else if ( timeDifference > 600 )
charge = 16;
else if ( timeDifference > 500 )
charge = 14;
else if ( timeDifference > 400 )
charge = 12;
else if ( timeDifference > 300 )
charge = 10;
else if ( timeDifference > 200 )
charge = 8;
else if ( timeDifference > 100 )
charge = 6;
else if ( timeDifference > 30 )
charge = 4;
else if ( timeDifference > 15 )
charge = 2;
else charge = 0;
} // end method calculateCharges
} // end class Parking
Java methods are blocks of code that perform a specific task. They are important because they allow us to break down complex problems into smaller, more manageable parts. This makes our code more organized, easier to read, and easier to maintain.
To call a method in Java, you need to use the method name followed by parentheses. If the method requires any parameters, they should be placed inside the parentheses. For example, if you have a method called calculateSum that takes two integers as parameters, you would call it like this: calculateSum(5, 10).
Yes, you can have multiple methods with the same name in Java as long as they have different parameter lists. This is called method overloading. Java will be able to differentiate between the methods based on the number, type, and order of the parameters.
You can use the return keyword to specify the value that the method should return. The return type of the method must match the type of the value being returned. For example, if you want to return an integer, the method should have a return type of int and you should use the return keyword followed by the integer value that you want to return.
Yes, a method can call itself in Java. This is known as recursion and it can be a useful technique for solving certain types of problems. However, it is important to make sure that the recursion has a base case to prevent an infinite loop.