Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java swing traffic violation

    I'm trying to make a small program using only java(swing) using CASE and try and catch, it's an application that will be used as an example for traffic violations in which the user can input speed limit, the speed at which offender was traveling and number of previous tickets that offender has received.

    The application needs an interface that will provide calculating charges and exit options. The application should calculate and display how many miles over the speed limit the offender was traveling, cost of speeding ticket and court cost. The program should calculate a charge of $20.00 for each mile per hour over the speed limit. The program should calculate court costs beginning at $74.80 for the first offense and increasing by $25.00 for each subsequent offense up to the third offense. (which will represent the maximum court cost).

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.


    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others, but try to help with fixing code that doesn't work. You'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,703
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by burkise View Post
    I'm trying to make a small program […].

    The application needs an interface […]. The application should calculate […]. The program should calculate […].
    And what’s your question now? Or are you just looking for someone to make it happen for you? If that’s the case we have a marketplace forum for such requests.

  • #4
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks I am new to this site so I didnt know however I did attempt the program

  • #5
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class TrafficViolations1
    {
    public static void main (String []args)
    {
    int speedLimit,speedOffenderTraveled,numTickets,speedOver;
    displayWelcome();
    speedLimit = getSpeedLimit();
    speedOffenderTraveled=getSpeedOffenderTraveled();
    numTickets=getpreviousTickets();
    speedOver=calcSpeedOver(speedLimit,speedOffenderTraveled);
    displayCharges(speedLimit,numTickets,speedOver);

    finish();
    }
    public static void displayWelcome() {
    System.out.println("\t\t Welcome to the Traffic Violation Calculator");
    System.out.println();
    }
    public static int getSpeedLimit (){
    String strspeedlimit;
    int speedLimit=0;
    boolean done = false;
    while (!done)
    {
    strspeedlimit=JOptionPane.showInputDialog(null,"Please Enter the Speed Limit","Input",JOptionPane.QUESTION_MESSAGE);
    if (strspeedlimit==null)
    {
    finish ();
    }
    else {
    try{
    speedLimit = Integer.parseInt(strspeedlimit);
    if (speedLimit<0)throw new NumberFormatException();
    else done=true;
    }
    catch (NumberFormatException e){
    JOptionPane.showMessageDialog(null, "Please Enter a Number that is 1 or Over.","Error", JOptionPane.INFORMATION_MESSAGE);
    }
    }
    }
    return speedLimit;
    }
    public static int getSpeedOffenderTraveled (){
    String strSpeedOffenderTraveled;
    int speedOffenderTraveled=0;
    boolean done = false;
    while (!done)
    {
    strSpeedOffenderTraveled=JOptionPane.showInputDialog(null,"Please Enter the Speed the Offender Traveled","Input",JOptionPane.QUESTION_MESSAGE);
    if (strSpeedOffenderTraveled==null)
    {
    finish ();
    }
    else {
    try{
    speedOffenderTraveled = Integer.parseInt(strSpeedOffenderTraveled);
    if (speedOffenderTraveled<0)throw new NumberFormatException();
    else done=true;
    }
    catch (NumberFormatException e){
    JOptionPane.showMessageDialog(null, "Please Enter a Number that is 1 or Over.","Error", JOptionPane.INFORMATION_MESSAGE);
    }
    }
    }
    return speedOffenderTraveled;
    }
    public static int getpreviousTickets()
    {
    String strNumTickets;
    int numTickets=0;
    strNumTickets = JOptionPane.showInputDialog(null, "Please Enter the Total Number of Previous Tickets");
    numTickets=Integer.parseInt(strNumTickets);
    return numTickets;
    }

    public static int calcSpeedOver (int speedLimit, int speedOffenderTraveled)
    {
    int speedOver;
    return speedOver = (speedOffenderTraveled - speedLimit);
    }
    public static double displayCharges (int speedLimit, int numTickets,int speedOver)
    {
    DecimalFormat twoDigits =new DecimalFormat("$#,000.0");
    double displayCharges=0.0;
    if (numTickets==0)
    displayCharges=((speedOver*20)+74.80);

    else if (numTickets>=1)
    if(numTickets <=3)
    displayCharges=((speedOver*20+25*numTickets)+74.80);
    else if (numTickets >3) displayCharges = (((speedOver*20) + 75 + 74.80));
    if (speedOver<=0)
    JOptionPane.showMessageDialog(null,"THE OFFENDER SHOULD NOT HAVE TO PAY ANYTHING");
    else if (speedOver>0) JOptionPane.showMessageDialog (null, "Because of " + numTickets
    + " Previous Tickets, and Going Over the Speed Limit by " + speedOver + " Your Total Cost is. " + twoDigits.format(displayCharges) + JOptionPane.INFORMATION_MESSAGE);
    return displayCharges;
    }
    public static void finish()
    {
    System.exit(0);
    }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •