Question

In: Computer Science

Write your code inside of SecondProgrammingExam.java including all Part I: Given two strings, a and b,...

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)
operable (Boolean)
cost (Double)
tires (array of 4 Tire objects)

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;
        }
    }

Solutions

Expert Solution

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


Related Solutions

Write your code inside of SecondProgrammingExam.java including all Part I: Given two strings, a and b,...
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...
Write a regular expression for the language of all strings over {a,b} in which every b...
Write a regular expression for the language of all strings over {a,b} in which every b is directly preceded by a, and may not be directly followed by more than two consecutive a symbols.
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Write a progrm that accepts two strings as input, then indicates if the two strings are...
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored. Sample runs are shown below. Use C++ Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal. Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
I didn't know , how to write this code // This file is part of www.nand2tetris.org...
I didn't know , how to write this code // This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/04/Fill.asm // Runs an infinite loop that listens to the keyboard input. // When a key is pressed (any key), the program blackens the screen, // i.e. writes "black" in every pixel; // the screen should remain fully black as long as the key is...
Can you complete the tasks inside the sample code. All the to-do task is inside the...
Can you complete the tasks inside the sample code. All the to-do task is inside the code commented out. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }node; /*Task 1 - Complete the function below, newNode() will return a tree node*/ node* newNode(int key){ } /*Task 2 - Complete the function below to return the size (number of elements stored) of a binary tree*/ int size(node* root){ }...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log in" AccessException: "Your account is forbidden from logging into this server" Any other exception: "Unknown...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Use virtual functions, pure virtual functions, templates, and exceptions wherever possible. Please explain the code. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log...
quiz 1 write your java code Have the function SearchingChallenge(strArr) read the array of strings stored...
quiz 1 write your java code Have the function SearchingChallenge(strArr) read the array of strings stored in strArr which will be a 4x4 matrix of the characters 'C', 'H', 'F', 'O', where C represents Charlie the dog, H represents its home, F represents dog food, and O represents and empty space in the grid. Your goal is to figure out the least amount of moves required to get Charlie to grab each piece of food in the grid by moving...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT