Jump to content

Simple Java in class


Drewyboyo

Recommended Posts

I'm taking computer programming in school and we're doing Java in JGrasp and I'm failing to add a value to a integer I already have defined.

The program is a ticket calculator for speeding

1. All tickets will start with a minimum amount of $75.
 
2. An additional $6 is charged for every mph over the posted speed limit.
 
3. If you are traveling over 30mph over the posted speed limit, an additional $160 is charged.
 
4. If the speeding occurred in a school zone, the cost of the ticket is doubled.
 
Failing on step 2, and I'm new to java so don't judge my stupidity lol.
 

System.out.println("What is the posted speed limit)? -->" );
int speedlimit = Expo.enterInt();
int ticketcost = 0;




System.out.println("How fast was the car travelling in mph? -->");
int speed = Expo.enterInt();
if(speed > speedlimit)
{
ticketcost+=75;
}
int excess = 0;

speedlimit is the speedlimit of the area,
speed is the speed of the car, and excess is every mile over,
was gonna write something along the lines of  speedlimit minus speed is the excess, and add multiplay excess by 6 and add it to my final ticket cost at the end of my program.
sorry if I wrote this badly, I don't know how to explain it and its messy but I've tried writing it like 4 different ways and im confused lol. (We're using some online book and I'm just copying his skeleton for my work if it looks bad)
 

 1 // Lab07Ast.java
 2 // The Speeding Ticket Program
 3 // This is the student, starting version of Lab 07A.
 4
 5
 6
public class Lab07Ast
 7 {
 8    public static void main(String args[])
 9    {
10       System.out.println("Lab 07A, 100 Point Version");
11          System.out.println();
12          System.out.println("The Speeding Ticket Program");
13          System.out.println();
14         System.out.println("By: Drew");  // Substitute your own name here.
15         System.out.println("\n==============================\n");
16        
17        
18        
19          System.out.println("What is the posted speed limit)? -->" );
20          int speedlimit = Expo.enterInt();
21          int ticketcost = 0;
22         
23         
24         
25         
26          System.out.println("How fast was the car travelling in mph? -->");
27          int speed = Expo.enterInt();
28          if(speed > speedlimit)
29          { 
30             ticketcost+=75;
31          }
32          int excess=((speed - speedlimit) * 6);
33                   if(excess >= speedlimit + 30)
34                   {
35                   ticketcost+=160;
36                   }
37          ticketcost+= excess;
38          System.out.println("Did the violation occur in a school zone?");
39          char school = Expo.enterChar();
40         
41          switch (school)
42          {
43             case 'Y':
44             case 'y':
45             ticketcost*= 2;
46              break;
47             case 'N':
48             case 'n':
49              ticketcost*= 1;
50             break;
51          }  
52         
53
54
            System.out.println("Ticket amount: " + ticketcost);
55         
56          
57
58
59
60
61
62
      System.out.println();
63    }
64 }

^ edit for tHE LAST TIME I actually got the riGHT ANSWER FINALLY
Edited by Drewyboyo
Link to comment
Share on other sites

int ticketPrice = 75 + ((speed - speedLimit) * 6)
//75 initial price
//speed(lets say 100) - speedLimit(lets say 80) = 20. 20 * 6 = 120, + 75 = 196

thanks homie, this class is actually teaching me a bit about the stuff i failed to grasp learning scripting on here char.gif

i had to edit like 50 times on this post bc i kept writing the wrong code like a retard thinking i was right so sry its final and fixed now and i got right answer but now its messy

  • Like 1
Link to comment
Share on other sites

 

I'm taking computer programming in school and we're doing Java in JGrasp and I'm failing to add a value to a integer I already have defined.

The program is a ticket calculator for speeding

1. All tickets will start with a minimum amount of $75.
 
2. An additional $6 is charged for every mph over the posted speed limit.
 
3. If you are traveling over 30mph over the posted speed limit, an additional $160 is charged.
 
4. If the speeding occurred in a school zone, the cost of the ticket is doubled.
 
Failing on step 2, and I'm new to java so don't judge my stupidity lol.
 

System.out.println("What is the posted speed limit)? -->" );

int speedlimit = Expo.enterInt();

int ticketcost = 0;

System.out.println("How fast was the car travelling in mph? -->");

int speed = Expo.enterInt();

if(speed > speedlimit)

{

ticketcost+=75;

}

int excess = 0;

speedlimit is the speedlimit of the area,
speed is the speed of the car, and excess is every mile over,
was gonna write something along the lines of  speedlimit minus speed is the excess, and add multiplay excess by 6 and add it to my final ticket cost at the end of my program.
sorry if I wrote this badly, I don't know how to explain it and its messy but I've tried writing it like 4 different ways and im confused lol. (We're using some online book and I'm just copying his skeleton for my work if it looks bad)
 

 

 1 // Lab07Ast.java

 2 // The Speeding Ticket Program

 3 // This is the student, starting version of Lab 07A.

 4

 5

 6 public class Lab07Ast

 7 {

 8    public static void main(String args[])

 9    {

10       System.out.println("Lab 07A, 100 Point Version");

11          System.out.println();

12          System.out.println("The Speeding Ticket Program");

13          System.out.println();

14         System.out.println("By: Drew");  // Substitute your own name here.

15         System.out.println("\n==============================\n");

16        

17        

18        

19          System.out.println("What is the posted speed limit)? -->" );

20          int speedlimit = Expo.enterInt();

21          int ticketcost = 0;

22         

23         

24         

25         

26          System.out.println("How fast was the car travelling in mph? -->");

27          int speed = Expo.enterInt();

28          if(speed > speedlimit)

29          { 

30             ticketcost+=75;

31          }

32          int excess=((speed - speedlimit) * 6);

33                   if(excess >= speedlimit + 30)

34                   {

35                   ticketcost+=160;

36                   }

37          ticketcost+= excess;

38          System.out.println("Did the violation occur in a school zone?");

39          char school = Expo.enterChar();

40         

41          switch (school)

42          {

43             case 'Y':

44             case 'y':

45             ticketcost*= 2;

46              break;

47             case 'N':

48             case 'n':

49              ticketcost*= 1;

50             break;

51          }  

52         

53

54             System.out.println("Ticket amount: " + ticketcost);

55         

56          

57

58

59

60

61

62       System.out.println();

63    }

64 }

^ edit for tHE LAST TIME I actually got the riGHT ANSWER FINALLY

 

Random question what state do you live in ?

Link to comment
Share on other sites

You could wrap it in a nice function like so:

    float getCost(int currentSpeed, int maxSpeed, boolean schoolZone){
        if (currentSpeed <= maxSpeed) {
            /* We arent speeding */
            return 0;
        }
        /* Base price of 75$ */
        float price = 75;
        /* Add 6$ per mph over speed */
        price += 6 * (currentSpeed - maxSpeed);

        if (currentSpeed - maxSpeed >= 30) {
            /* 30 mph or more to fast, add 160$ */
            price += 160;
        }
        
        if (schoolZone) {
            /* We are in a school zone, double the price */
            price *= 2;
        }
        return price;
    }

It also makes uses of floats, perfect for decimals in money

Edited by Abuse
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...