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 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. |
(1.) mixString()
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String 1 : ");
String str1 = sc.nextLine();
System.out.print("Enter String 2 : ");
String str2 = sc.nextLine();
String output = mixString(str1, str2);
System.out.println("Your Mixed String : " + output);
}
public static String mixString(String str1, String str2)
{
int minlength = Math.min(str1.length(), str2.length());
String mixedString = "";
for (int i = 0; i < minlength; i++)
{
mixedString = mixedString + str1.charAt(i) + str2.charAt(i);
}
mixedString = mixedString + str2.substring(minlength) + str1.substring(minlength);
return mixedString;
}
}
Output:
(2.) createArray()
public class Main
{
public static void main(String[] args) {
int[] arr1 = {1, 2, 4, 7, 100, 3, 2};
int[] arr2 = {9, 100, 200, 300, 5};
createArray(arr1);
createArray(arr2);
}
public static void createArray(int[] array)
{
int[] newArray = new int[array.length];
for (int i=0; i<array.length; i++)
{
if (array[i] < 5)
{
newArray[i] += array[i];
}
}
System.out.print("{");
for (int i=0; i<newArray.length; i++)
{
if (newArray[i]!=0)
{
System.out.print(newArray[i] + ", ");
}
}
System.out.println("}");
System.out.println();
}
}
Output:
(3.) checkPressure()
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter Brand : ");
String brand = sc.nextLine();
System.out.print("Enter Current PSI : ");
double psi = sc.nextDouble();
checkPressure(brand, psi);
}
public static void checkPressure(String brand, double psi)
{
if (psi < 45)
{
System.out.println("\nYour " + brand + " tire has two low pressure");
}
else
{
System.out.println("\nYour " + brand + " tire has Acceptable pressure");
}
}
}
Output:
Thumbs Up Please !!!