In: Computer Science
Write your code inside of SecondProgrammingExam.java including all
Part I:
Given two strings, a and b, create a bigger string made of the first char of a, the first char of b, the second char of a, the second char of b, and so on. Any leftover chars go at the end of the result. mixString("abc", "xyz") → "axbycz" mixString("Hi", "There") → "HTihere" mixString("xxxx", "There") → "xTxhxexre" |
Part II:
Given an array of integers create an array of integers in the same order with all the items less than 5. If the element of the array is greater than 5 or equal to 5 ignore it from the new integer array. int[] arr1 = {1, 2, 4, 7, 100, 3, 2}; int [] arr2 = {9, 100, 200, 300, 5}; createArray(arr1) → {1, 2, 4, 3, 2}; createArray(arr2) → {}; //nothing is less than 5! |
Part III:
Create a class Tire with the following: brand (String) currentPSI (Double) Your constructor should look like this: public Tire(String brand, double currentPSI) Write a public method checkPressure() that prints out whether the current PSI is less than 45. If the current PSI is less than 45, print out "Too low." Otherwise print out "Acceptable pressure." Create a class Car with the following: horsepower (Integer) Your constructor should look like this: public Car(int horsepower, boolean operable, double cost) Write a public method isPowerful() that returns true if the car's horsepower is 300 or greater or false if the horsepower is less than 300. To make testing easier, I've added the main method to the starter code. Include that when you submit your project. |
starter code public static void main(String[] args) { /* The main method is already completed for you. You do not need to touch this. */ Scanner scanner = new Scanner(System.in); switch(scanner.nextInt()){ case 1: System.out.println(mixString(scanner.next(), scanner.next())); break; case 2: int size = scanner.nextInt(); int arr[] = new int[size]; for(int i = 0; i < size; i++){ arr[i] = scanner.nextInt(); } int [] output = createArray(arr); System.out.println(Arrays.toString(output)); break; case 3: Tire t = new Tire(scanner.next(), scanner.nextDouble()); t.checkPressure(); Car a = new Car(scanner.nextInt(), scanner.nextBoolean(), scanner.nextDouble()); System.out.println(a.isPowerful()); break; } }
Ok, so the code is attached below.
First, read and run the program.
And, then if you think anything is missing or should be done by another way then do let me know.
Or, if you get any doubt feel free to drop that in the comment section.
Code:
import java.util.*;
class Tire{
private String brand;
private double currentPSI;
public Tire(String next, double nextDouble) {
this.brand=next;
this.currentPSI=nextDouble;
}
public void checkPressure() {
if(this.currentPSI<45) {
System.out.println("Too low");
}
else {
System.out.println("Acceptable pressure");
}
}
}
class Car{
private int horsepower;
private boolean operable;
private double cost;
private Tire[] tires= {new Tire("CEAT", 50),new
Tire("CEAT", 49),new Tire("MRF", 55),new Tire("CEAT", 55)};
public Car(int nextInt, boolean nextBoolean, double
nextDouble) {
this.horsepower=nextInt;
this.operable=nextBoolean;
this.cost=nextDouble;
}
public boolean isPowerful() {
if(this.horsepower<300) {
return
false;
}
return true;
}
}
public class SecondProgrammingExam{
private static int[] createArray(int[] arr) {
int len=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<5)
{
len++;
}
}
int[] result=new int[len];
if(len==0) {
return
result;
}
int j=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<5)
{
result[j]=arr[i];
j++;
}
}
return result;
}
private static char[] mixString(String next, String
next2) {
int
len=next.length()>next2.length()?next.length():next2.length();
char[] c=new
char[next.length()+next2.length()];
int j=0;
for(int i=0;i<len;i++) {
try {
c[j]=next.charAt(i);
j++;
}
catch (Exception
e) {
}
try {
c[j]=next2.charAt(i);
j++;
}
catch (Exception
e) {
}
}
return c;
}
public static void main(String[] args) {
/*
The main method is already completed for you. You do not need to
touch this.
*/
Scanner scanner = new Scanner(System.in);
switch(scanner.nextInt()){
case 1:
System.out.println(mixString(scanner.next(),
scanner.next()));
break;
case 2:
int size = scanner.nextInt();
int arr[] = new int[size];
for(int i = 0; i < size; i++){
arr[i] = scanner.nextInt();
}
int [] output = createArray(arr);
System.out.println(Arrays.toString(output));
break;
case 3:
Tire t = new Tire(scanner.next(), scanner.nextDouble());
t.checkPressure();
Car a = new Car(scanner.nextInt(), scanner.nextBoolean(),
scanner.nextDouble());
System.out.println(a.isPowerful());
break;
}
}
}
# PART 1
# PART 2
# PART 3