I need to create a driver program that says if I can fight a ticket or just pay it off. My problem is calling in another class called ticket to create an if else statement. I created an if else statement that will only say to fight the ticket on the last ticket...
(here is the problem im stuck on) **The problem: You are receiving speeding tickets which are automatically generated by a camera. You can FIGHT the ticket if any of the following are true:
• Your recorded speed is <= 11 mph over the limit
what am i doing wrong?
import java.util.*; //scanner
public class MinilabUsingClasses
{
public static void main (String [] args)
{
Scanner kb = new Scanner(System.in); //creating new instance of scanner
int numTickets; //variable for number of tickets
String name; //variable for name
System.out.print("How many tickets do you have? " ); //ask user for numtickets
numTickets = kb.nextInt();
System.out.print("\nWhat is your name? " ); //ask user for name
name = kb.next();
System.out.println("\n");
for (int i= 1; i <= numTickets; i++) // use for loop to bring in random ticket info
{
Ticket currentTicket = new Ticket(name);
System.out.println(currentTicket.toString());
}
int newSpeed = 0;
if (newSpeed <= 11)
{
System.out.println ("Fight!");
}
else if (newSpeed > 11)
{
System.out.println("Pay :(");
}
}
}
//This class will describe a theoretical "speed camera ticket"
import java.util.*; //for random number generator
public class Ticket
{
//constant
private final int LIMIT = 55;
//data
private String name;
//private int limit;
private int speed;
private String cameraID;
private String location;
//default constructor - creates a ticket with the name "Bill Gates"
public Ticket()
{
this("Bill Gates"); //just turns around and calls constructor with single arg
}
//parameterized constructor - this will set all values. limit and location
//will be constant and the rest will have some randomness built in
public Ticket(String theName)
{
//random number generator
Random generator = new Random();
//local variables
int rand, len;
char randomChar;
//set the name data value
name = theName;
//name will have some randomness
for (int i=0; i<name.length(); i++)
{
rand = generator.nextInt(10); //random int from 0-9
if (rand < 3) //3/10 of the time change lowercase <-> uppercase
if (name.charAt(i) >= 'a' && name.charAt(i) <= 'z')
{
//lowercase -> uppercase
name = name.substring(0,i) + (char)(name.charAt(i)-32) + name.substring(i+1);
}
else if (name.charAt(i) >= 'a' && name.charAt(i) <= 'z')
{
//uppercase -> lowercase
name = name.substring(0,i) + (char)(name.charAt(i)+32) + name.substring(i+1);
}
rand = generator.nextInt(21); //random int from 0-20
if (rand < 1 && name.charAt(i) != ' ') //1/20 of the time put in a typo (but not for blanks)
{
randomChar = (char)(generator.nextInt(94)+33); //get a randomChar in the ASCII table (33-126)
name = name.substring(0,i) + randomChar + name.substring(i+1); //put it in
}
}
//it is programmed for constant location and speed limit
location = "I-10 and 48th Street";
//generate pseudorandom "cameraID"
rand = generator.nextInt(10); //int between 0-9
if (rand < 2)
cameraID = "A"; //starts with A 2/10 of the time
else
cameraID = "B";
//middle digits of varying length
len = generator.nextInt(4)+4; //length this part of the ID (3-7 digits)
for (int i=0; i<len; i++)
cameraID = cameraID + (char)(generator.nextInt(10)+48); //concatenate a random digit
//ends with 35 2/10 of the time
rand = generator.nextInt(10);
if (rand < 2)
cameraID = cameraID + "35"; //concatenate
//speed is a random number between 6 - 20 mph over the limit
rand = (int)(Math.random()*20);
speed = LIMIT + 6 + rand;
}
//getLimit - returns the speed limit the camera was set at
public int getLimit()
{
return LIMIT;
}
//getSpeed - returns the driver's speed
public int getSpeed()
{
return speed;
}
//setSpeed - sets the speed to whatever is passed in
public void setSpeed(int newSpeed)
{
speed = newSpeed;
}
//getName - returns the driver's name
public String getName()
{
return name;
}
//get getCameraID - returns the cameraNumber
public String getCameraID()
{
return cameraID;
}
//setCameraID - sets it to whatever is passed in
public void setCameraID(String newID)
{
cameraID = newID;
}
//toString - returns its representation as a String
public String toString()
{
String str = "\nName: " + name + "\nLimit: " + LIMIT;
str += "\nSpeed: " + speed + "\nCamera: " + cameraID;
str += "\nLocation: " + location;
return str;
}
}
Aucun commentaire:
Enregistrer un commentaire