Question

In: Computer Science

Name the project pa3 [Method 1] In the Main class, write a static void method to...

Name the project pa3

[Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit.

1: All work and no play makes Jack a dull boy.

2: All work and no play makes Jack a dull boy.

3: All work and no play makes Jack a dull boy.

4: All work and no play makes Jack a dull boy.

[Method 2] In the Main class, write a static method that accepts an array of floating point parameters. The three elements in the array can be labeled as a, b, and c from first to last. The method computes and returns the results of the quadratic equation in a separate two-element array given by the following equation: −b ± √( b^2 − 4 · a · c) / (2 · a), as long as the discriminant (b^2 − 4 · a · c) is non-negative. Otherwise, return a reference to an array object with zeros in both elements.

[Method 3] In the Main class, write a static void method that receives three Strings and prints them in reverse dictionary order, Z to A. Tips: Should the result of the following example be positive: callingString.compareTo(otherString), callingString will appear after otherString in the dictionary. There are six possible orderings of 3 items, so you should not need more than 5 nested if statements. Another option is to place the Strings into an array, use the Arrays.sort static method, and traverse the array backwards.

[Method 4] In the Main class, write a static method that receives a String parameter named option. The method then returns one of the following String literals based on the return value of option’s length() function. When option has fewer than 10 characters, return “too small”. When option has more than 10 characters, return “too big”. When option has exactly 10 characters, return “just right”.

In the main method, write the Java code to receive the needed actual parameters from the user for each method and call each method. For instance, the three numbers required for Method 2 should be read from the user. Also include the appropriate prompts instructing the user what to enter.

Requirements

1) A reference variable and instance object of class java.util.Scanner must be used to read the input from the user. 2) Identifiers must be descriptive (i.e., must self document).

3) Indention of all code blocks (compound statements, code inside braces), including single statements following selection or while statements, is required.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

Let me know for any help with any other questions.

Thank You!

===========================================================================

public class Main {

    // Question 3
    public static void repeat4(String line) {

        for (int count = 1; count <= 4; count++) {

            System.out.println(count + ": " + line);
        }
    }


    // Question 4
    public static double[] getRoots(double a, double b, double c) {

        double[] roots = {0.0, 0.0};
        double discriminant = b * b - 4 * a * c;
        if (discriminant < 0) {
            return roots;
        }

        discriminant = Math.sqrt(discriminant);

        roots[0] = (-b + discriminant) / (2 * a);
        roots[1] = (-b - discriminant) / (2 * a);
        return roots;

    }


    // Question 5
    public static void reversePrint(String a, String b, String c) {

        if (a.compareTo(b) < 0 && a.compareTo(c) < 0) {
            if (b.compareTo(c) < 0) {
                System.out.println(c);
                System.out.println(b);
                System.out.println(a);
            } else {
                System.out.println(b);
                System.out.println(c);
                System.out.println(a);
            }
        } else if (b.compareTo(a) < 0 && b.compareTo(c) < 0) {
            if (a.compareTo(c) < 0) {
                System.out.println(c);
                System.out.println(a);
                System.out.println(b);
            } else {
                System.out.println(a);
                System.out.println(c);
                System.out.println(b);
            }
        } else {
            if (a.compareTo(b) < 0) {
                System.out.println(b);
                System.out.println(a);
                System.out.println(c);
            } else {
                System.out.println(a);
                System.out.println(b);
                System.out.println(c);
            }
        }

    }

    // 6
    public static String optionString(String s) {
        if (s == null || s.length() < 10) return "too small";
        else if (s.length() == 10) return "just right";
        else return "too big";
    }

    // 7

    public static void getInput() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Method 1");
        System.out.print("Enter a line to repeat: ");
        String line = scanner.nextLine();

        repeat4(line);

        System.out.println("Method 2");
        System.out.print("Enter value of a: ");
        double a = scanner.nextDouble();

        System.out.print("Enter value of b: ");
        double b = scanner.nextDouble();

        System.out.print("Enter value of c: ");
        double c = scanner.nextDouble();
        scanner.nextLine();
        double[] roots = getRoots(a, b, c);

        System.out.println("Root 1: " + roots[0] + ", Root 2:" + roots[1]);


        System.out.println("Method 3");
        System.out.print("Enter first string: ");
        String first = scanner.nextLine();
        System.out.print("Enter second string: ");
        String second = scanner.nextLine();
        System.out.print("Enter third string: ");
        String third = scanner.nextLine();

        reversePrint(first, second, third);


        System.out.println("Method 4");
        System.out.print("Enter option string: ");
        String option = scanner.nextLine();
        System.out.println(optionString(option));
    }


    public static void main(String[] args) {


        getInput();


    }
}

===========================================================================


Related Solutions

3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT