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();
}
}
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!
Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...
**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...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
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 + "...
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...
******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...
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...
Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...
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 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 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...