Question: For this java program, is there any way to convert the while (boolen) loops to a do-while loop and still have it work the same?
For this java program, is there any way to convert the while (boolen) loops to a do-while loop and still have it work the same? Also I need a currency in a mask for my output and don't know how to do that.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Traffic
{
public static void main(String[] args)
{
//declare class variables
double fine, courtCosts, ticket;
int speedLimit, offenderSpeed, previousTickets, overLimit;
System.out.println(" *****Ticket Calculator*****");
//call methods
speedLimit = getLimit();
offenderSpeed = getDriverSpeed();
if (offenderSpeed <= speedLimit)
{
JOptionPane.showMessageDialog(null,"No violation", "Traffic Ticket",JOptionPane.INFORMATION_MESSAGE);
finish();
}
previousTickets = getTickets();
overLimit = offenderSpeed - speedLimit;
fine = overLimit * 20.00;
courtCosts = getCosts(previousTickets);
ticket = fine + courtCosts;
output(fine, courtCosts, ticket);
finish();
}
//The getLimit() method asks the user to input a the speed limit.
public static int getLimit()
{
//declare method variables
int limit = 0;
String answer;
boolean loop=true;
//Loop till user enters a valid value
while(loop)
{
//Reading value from user
answer = JOptionPane.showInputDialog(null,"Enter the speed limit:");
//Converting to integer
limit = Integer.parseInt(answer);
//If limit less than 0
if(limit < 0)
{
//Printing a message
JOptionPane.showMessageDialog(null,"Your entry must be an integer greater than 0.","Error",JOptionPane.INFORMATION_MESSAGE);
}
else
{
//Set loop to false
loop = false;
}
}
return limit;
}
//The getDriverSpeed() method asks the user to input the offender's speed.
public static int getDriverSpeed()
{
//declare method variables
int speed = 0;
String answer;
boolean loop=true;
//Loop till user enters a valid value
while(loop)
{
//Reading value from user
answer = JOptionPane.showInputDialog(null,"Enter the offenders speed:");
//Converting to integer
speed = Integer.parseInt(answer);
//If limit less than 0
if(speed < 0)
{
//Printing a message
JOptionPane.showMessageDialog(null,"Your entry must be an integer greater than 0.","Error",JOptionPane.INFORMATION_MESSAGE);
}
else
{
//Set loop to false
loop = false;
}
}
return speed;
}
//The getTickets() method retrieves number of tickets.
public static int getTickets()
{
//declare method variables
int tickets = 0;
String answer;
boolean loop=true;
//Loop till user enters a valid value
while(loop)
{
//Reading value from user
answer = JOptionPane.showInputDialog(null,"Enter the number of previous tickets:");
//Converting to integer
tickets = Integer.parseInt(answer);
//If limit less than 0
if(tickets < 0)
{
//Printing a message
JOptionPane.showMessageDialog(null,"Your entry must be an integer greater than 0.","Error",JOptionPane.INFORMATION_MESSAGE);
}
else
{
//Set loop to false
loop = false;
}
}
return tickets;
}
//The getCosts() method returns the court costs.
public static double getCosts(int tickets)
{
double costs = 0.0;
//0-1 previous ticket - $74.80
if(tickets <= 1)
{
costs = 74.80;
}
//2 previous tickets - $99.80
else if(tickets == 2)
{
costs = 99.80;
}
//3 or more - $124.80
else
{
costs = 124.80;
}
return costs;
}
//The output() method displays the cost of the ticket.
public static void output(double dFine, double dCost, double dTicket)
{
//Forming a message string
String mesg = "Your fine of $" + String.format("%.2f",dFine) + " plus your court costs of $" + String.format("%.2f",dCost) + " is $" + String.format("%.2f",(dCost + dFine));
//Printing a message
JOptionPane.showMessageDialog(null, mesg,"Traffic Ticket",JOptionPane.INFORMATION_MESSAGE);
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
