Below shows a main program with one array that can insert 100 random numbers & a
ID: 3757293 • Letter: B
Question
Below shows a main program with one array that can insert 100 random numbers & also has a sequential search method that shows if a specific value is found inside an array. Using generics, now create an array of ten cars & check if a given car is in the array.
Write a generic method to find the maximum value of the array to return its maximum value.Then write a generic method to display the array.
(main)
import java.util.Random;
public class Application1{
public static void main(String[] args){
Random x = new Random();
Integer[] array = new Integer[100];
for(int i = 1; i < array.length; i++)
{
array[i] = x.nextInt(10)+1;
System.out.print(array[i] + " ");
System.out.println();
}
int x = 2;
System.out.println("Is 2 inside the array? " + sequentialSearch(array, x));
}
public static <T> boolean sequentialSearch(T[] array, T value)
{
int index;
boolean found;
index =0;
found = false;
while(!found && index <array.length)
{
if(array[index]==(value))
{
found = true;
}
index++;
}
return found;
}
}
-------------
(Car class)
public class Car implements CarInter, Comparable{
private int speed;
private int tank;
private String model;
private int [] data;
public Car()
{
tank = 5;
speed = 0;
model = "newCar";
}
public int compareTo(Object c)
{
Car m = (Car) c;
if (tank > m.tank)
return 1;
else if (tank == m.tank)
return 0;
else
return -1;
}
public void goFast(int s)
{
speed += s;
if (speed > 65)
speed = 65;
}
@Override
public String toString()
{
String st = "";
st = st + model + " " + tank;
return st;
}
public void pumpGas(int t)
{
tank += t;
if (tank > 15)
tank = 15;
}
public void goSlow(int s)
{
speed = speed - s;
if (speed < 0)
speed = 0;
}
public void displaySpeed()
{
System.out.println("speed = " + speed);
}
---------
(Car interface)
public interface CarInter {
public void goFast(int s);
public void goSlow(int s);
public void pumpGas(int t);
}
Explanation / Answer
//-------------------------------------- // Main Class (Application1) //======================================= import java.util.Random; public class Application1{ public static void main(String[] args) { Car c = new Car(); Random x = new Random(); Integer[] array = new Integer[100]; for (int i = 1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.