Drewyboyo Posted January 20, 2017 Share Posted January 20, 2017 (edited) 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 5354 System.out.println("Ticket amount: " + ticketcost);55 56 575859606162 System.out.println();63 }64 } ^ edit for tHE LAST TIME I actually got the riGHT ANSWER FINALLY Edited January 20, 2017 by Drewyboyo Quote Link to comment Share on other sites More sharing options...
The Hero of Time Posted January 20, 2017 Share Posted January 20, 2017 int ticketPrice = 75 + ((speed - speedLimit) * 6) //75 initial price //speed(lets say 100) - speedLimit(lets say 80) = 20. 20 * 6 = 120, + 75 = 196 1 Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted January 20, 2017 Author Share Posted January 20, 2017 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 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 1 Quote Link to comment Share on other sites More sharing options...
Orange Posted January 20, 2017 Share Posted January 20, 2017 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 ? Quote Link to comment Share on other sites More sharing options...
Drewyboyo Posted January 20, 2017 Author Share Posted January 20, 2017 Random question what state do you live in ? arkansas ! Quote Link to comment Share on other sites More sharing options...
Orange Posted January 20, 2017 Share Posted January 20, 2017 arkansas ! o, I thought you were from Alabama Quote Link to comment Share on other sites More sharing options...
Abuse Posted January 21, 2017 Share Posted January 21, 2017 (edited) 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 January 21, 2017 by Abuse 1 Quote Link to comment Share on other sites More sharing options...