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 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.

Solutions

Expert Solution

(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 !!!


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 am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT