Question

Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

Can someone help me with my Java code error!

Domain

package challenge5race;

import java.util.Random;


public class Car {
  
private int year;
private String model;
private String make;
int speed;

public Car(int year, String model, String make, int speed) {
this.year = year;
this.model = model;
this.make = make;
this.speed = speed;
}

public Car() {
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

@Override
public String toString() {
return "Car{" + "year=" + year + ", model=" + model + ", make=" + make + ", speed=" + speed + '}';
}

public void accelerate()
{
// add a random number between 5 and 70, to the speed field
Random myRan = new Random();

int speedran = myRan.nextInt((70 - 5) + 1) + 5;
speed = speed + speedran;
}

public void brake()
{
// subtract a random number between 5 and 30, to the speed field
Random myRan = new Random();

int speedran = myRan.nextInt((30 - 5) + 1) + 5;
speed = speed - speedran;
  
if (speed < 5)
{
speed = 0;
}

}

  
  
}

Driver

package challenge5race;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Challenge5Race {

/**
* @param args the command line arguments
*/
static Car [] carArray1;
static Car [] carArray2;
public static void main(String[] args) throws IOException {
// TODO code application logic here
  
createArraysOfCars();
raceArraysOfCars();
  
}
  
public static void createArraysOfCars() throws IOException
{
//createWinnerArray(); -- for each record in winner file, create a car object and store in winner array
// createLoserArray(); -- for each record in loser file, create a car object and store in loser array
// make sure to close both files
  
  
int array1Size = 0;
File aFile = new File("WinnerCarSet1.txt"); //doesn't open file
Scanner inFile = new Scanner (aFile); //opens file
int aYear; //fields
String aModel;
String aMake;
Car aCar;
int index = 4;
int speed;
  
array1Size = inFile.nextInt();
carArray1= new Car [array1Size];

  
while(inFile.hasNext()) //create a loop
{
inFile.nextLine(); // count them
aYear = inFile.nextInt();
aModel = inFile.next();
aMake = inFile.next();
speed = inFile.nextInt();
aCar = new Car (aYear,aModel,aMake,speed);
carArray1[index] = aCar;
index ++;
}
  

inFile.close();
  
  
File aFile2 = new File ("loserCarSet2.txt");
Scanner inFile2 = new Scanner (aFile2);
  
int array2Size = 0;
int index2= 4;
array2Size = inFile2.nextInt();
  
carArray2 = new Car [array2Size];
  
while(inFile.hasNext())
{
inFile2.nextLine();
aYear = inFile2.nextInt();
aModel = inFile2.next();
aMake = inFile2.next();
speed = inFile2.nextInt();
aCar = new Car (aYear,aModel,aMake,speed);
carArray2[index2] = aCar;
index ++;
}
inFile.close();
  
  
  
}
  
public static void raceArraysOfCars() throws IOException
{
//open both files as output, to clear contents
//write the number 4 as the first record in both files.
  
//Outer loop for indexes 0 to length of array1
// nested loop - race 5 times
// call the accelerate and brake method
// for the car at the outerloop index location,
// using both cars from both arrays
// after the nested loop, determine winner of 5 laps, and
// state it, and write the winner car in the winner file,
// and write the loser car in the loser file.
  
//close both files
PrintWriter pw1 = new PrintWriter("winnerCarSet1.txt");
PrintWriter pw2 = new PrintWriter("loserCarSet2.txt");

for (int i = 0; i < carArray1.length && i < carArray2.length; i++) {

// lap race
for (int j = 0; j < 5; j++) {
carArray1[i].accelerate();
carArray1[i].brake();

carArray1[i].accelerate();
carArray2[i].brake();

System.out.println("Car1 " + carArray1[i]);
System.out.println("Car2 " + carArray2[i]);
}

if (carArray1[i].speed > carArray2[i].speed) {
pw1.println(carArray1[i].toString());
pw2.println(carArray2[i].toString());
} else {
pw1.println(carArray2[i].toString());
pw2.println(carArray1[i].toString());
}

}

pw1.close();
pw2.close();
}
  
  
  
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

class Car {
        int year;
        String model;
        String carMake;
        int speed;
        private String make;

        public Car() {
                year = 2018;
                model = "Malibur";
                carMake = "Chevy";
                speed = 90;
        }

        // parameters for constructors
        public Car(int year, String model, String make, int speed) {
                this.year = year;
                this.model = model;
                this.carMake = make;
                this.speed = speed;
        }

        public int getYear() {
                return year;
        }

        public void setYear(int year) {
                this.year = year;
        }

        public String getModel() {
                return model;
        }

        public void setModel(String model) {
                this.model = model;
        }

        public String getCarMake() {
                return carMake;
        }

        public void setCarMake(String carMake) {
                this.carMake = carMake;
        }

        public int getSpeed() {
                return speed;
        }

        public void setSpeed(int speed) {
                this.speed = speed;
        }

        // -----------------------------------------------------------

        @Override
        public String toString() {
                return "" + year + " " + model + " " + carMake + " " + speed;
        }

        // -------------------------------------------------------------
        public void accelerate() {
                Random myRan = new Random();

                int speedr = myRan.nextInt((70 - 5) + 1) + 5;
                speed = speed + speedr;
        }

        public void brake() {
                Random myRan = new Random();

                int speedr = myRan.nextInt((30 - 5) + 1) + 5;
                speed = speed - speedr;

                if (speed < 5) {
                        speed = 0;
                }
        }
}

public class Challenge5Race {

        public static Car carArray1[];
        public static Car carArray2[];
        static String file1 = "winnerCarSet1.txt";
        static String file2 = "loserCarSet2.txt";

        public static void createArraysOfCars() throws FileNotFoundException {
                Scanner reader1 = new Scanner(new File(file1));
                Scanner reader2 = new Scanner(new File(file2));

                int array1Size = Integer.parseInt(reader1.nextLine());
                carArray1 = new Car[array1Size];

                int array2Size = Integer.parseInt(reader2.nextLine());
                carArray2 = new Car[array2Size];

                for (int i = 0; i < array1Size; i++) {
                        String line = reader1.nextLine();

                        String tokens[] = line.split(" ");
                        carArray1[i] = new Car(Integer.parseInt(tokens[0]), tokens[1], tokens[2], Integer.parseInt(tokens[3]));
                }

                for (int i = 0; i < array2Size; i++) {
                        String line = reader2.nextLine();

                        String tokens[] = line.split(" ");
                        carArray2[i] = new Car(Integer.parseInt(tokens[0]), tokens[1], tokens[2], Integer.parseInt(tokens[3]));
                }
        }

        public static void raceArraysOfCars() throws IOException {
                // open both files as output, to clear contents
                // Use the Print Writer to clear the contents
                // write the number 4 as the first record in both files.

                // Outer loop for indexes 0 to length of array1
                // nested loop - race 5 times
                // call the accelerate and brake method
                // for the car at the outerloop index location,
                // using both cars from both arrays
                // after the nested loop, determine winner of 5 laps, and
                // state it, and write the winner car in the winner file,
                // and write the loser car in the loser file.

                // close both files
                PrintWriter pw1 = new PrintWriter("winnerCarSet1.txt");
                PrintWriter pw2 = new PrintWriter("loserCarSet2.txt");

                for (int i = 0; i < carArray1.length && i < carArray2.length; i++) {

                        // lap race
                        for (int j = 0; j < 5; j++) {
                                carArray1[i].accelerate();
                                carArray1[i].brake();

                                carArray1[i].accelerate();
                                carArray2[i].brake();

                                System.out.println("Car1 " + carArray1[i]);
                                System.out.println("Car2 " + carArray2[i]);
                        }

                        if (carArray1[i].speed > carArray2[i].speed) {
                                pw1.println(carArray1[i].toString());
                                pw2.println(carArray2[i].toString());
                        } else {
                                pw1.println(carArray2[i].toString());
                                pw2.println(carArray1[i].toString());
                        }

                }
                
                pw1.close();
                pw2.close();
        }

        public static void main(String args[]) throws IOException {
                createArraysOfCars();
                raceArraysOfCars();
        }
}

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame {...

    package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame { private String userChoice, computerChoice;    public RPSGame() { userChoice = "rock"; computerChoice = "rock"; }    public String getUserChoice() { return userChoice; }    public String getComputerChoice() { return computerChoice; }    public void setUserChoice(String aUserChoice) { userChoice = aUserChoice; }    public void setComputerChoice(String aComputerChoice) { computerChoice = aComputerChoice; }    public String toString() { return "User Choice: " + userChoice + "...

  • Show the compilation of Programming Challenge below in separate header and implementation files and show a...

    Show the compilation of Programming Challenge below in separate header and implementation files and show a UML diagram of this class. #include “Car.h” Int main() { Car car(2015, “Volkswagon”); Cout<<”Car’s Make: “ << car.getMake() <<endl; Cout<< “Car’s Year:” << car.getYear() <<endl; Cout<< “Car’s Speed : “ <<car.getSpeed()<<endl; Cout<< endl; For(int I =0; I < 5; i++) { Car.acceleration(); Cout<< “Speed after accelerate: “ << car.getSpeed() << endl; } For(int I = 0; i < 5; i++) { Car.brake(); Cout <<...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public...

    Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public static void main(String[] args) { Tree_ctor_test(); Tree_insert1_test(); Tree_insert3_test(); Tree_insertMany1_test(); Tree_insertMany2_test(); Tree_insertMany3_test(); Tree_insertMany4_test(); } public static void Tree_ctor_test() { Tree<Integer> t = new BSTree<Integer>(); assertEqual("", t.toString(), "Tree_ctor_test: toString", false); assertEqual(false, t.contains(0), "Tree_ctor_test: contains", false); } public static void Tree_insert1_test() { Tree<Integer> t = new BSTree<Integer>(); t.insert(1); assertEqual("1 ", t.toString(), "Tree_insert1_test: toString", false); assertEqual(false, t.contains(0), "Tree_insert1_test: contains", false); assertEqual(true, t.contains(1), "Tree_insert1_test: contains", false); } public static...

  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT