Question

In: Computer Science

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 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: (quadratic formula) 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.

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

6. [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 size() function. When option has fewer than 10 characters, return “too small”. When option has more than 10 characters, return “to big”. When option has exactly 10 characters, return “just right”.

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

Additional Requirements:

The following coding and implementation details must be present in your solution to receive full credit for Programming Assignment #3.

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.

4. Submissions not including a complete IntelliJ project folder in a ZIP file will not be graded.

Solutions

Expert Solution

Hello and Hope you're doing fine. The following solution was compiled in JDK11 and seems to adhere to your requirements.

import java.util.Collections;
import java.util.Arrays;
import java.util.Scanner;

public class MyClass {
  
    static void printLoop(){
        for(int i=1; i<5; i++){
            System.out.println(i+": All work and no play makes Jack a dull boy.");
        }
    }
  
    static float[] quadraticEquation(float[] coefficients){
      
        float a = coefficients[0];
        float b = coefficients[1];
        float c = coefficients[2];
      
        float discriminant = b*b - 4 * a * c;
        if(discriminant > 0){
          
            float root1 = (-b + (float)Math.sqrt(discriminant)) / 2f * a;
            float root2 = (-b - (float)Math.sqrt(discriminant)) / 2f * a;
          
            return (new float[]{root1, root2});
        }
        else{
            return (new float[]{0f,0f});
        }
      
    }
  
    static void stringReverseSort(String[] words){
      
        Arrays.sort(words, Collections.reverseOrder());
      
        for(int i=0;i<3;i++){
            System.out.println(words[i]);
        }
    }
  
    static String stringLength(String option){
        int length = option.length();
        if(length>10){
            return("too big");
        }
        else if(length<10){
            return("too small");
        }
        else{
            return("just right");
        }
    }
  
  
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
      
        //Loop to print All work and no play makes Jack a dull boy four times, counted.
        printLoop();
      
        System.out.println("Enter quadratic equation coefficients as a b and c:\na:");
        float a = sc.nextFloat();
        System.out.println("b:");
        float b = sc.nextFloat();
        System.out.println("c:");
        float c = sc.nextFloat();
        //Quadratic equation
        float[] roots = quadraticEquation(new float[]{a, b, c});
        System.out.println("Roots of Quadratic equation are: "+roots[0]+" "+roots[1]);
      
      
        System.out.println("Enter three strings to sort:\nString 1:");
        sc.nextLine();
        String one = sc.nextLine();
        System.out.println("String 2:");
        String two = sc.nextLine();
        System.out.println("String 3:");
        String three = sc.nextLine();
        //Call to print strings in reverse dictionary order
        stringReverseSort(new String[]{one,two,three});
      
        //StringLengthCheck
        System.out.println("Enter string to check length:\nString:");
        String checker = sc.nextLine();
        String lengthOutput = stringLength(checker);
        System.out.println(lengthOutput);
      
    }
}

Hope this helped. Thanks and Have a nice day.

NB: If you require so, you could reverse sort the array without importing collections by printing it in reverse.

The extra sc.nextLine() call after a float read is to discard the extra trailing newline character '\n' that nextFloat() does not read. You could use the skip function built into scanner. Both serves the same purpose.

Happy Learning!


Related Solutions

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]...
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...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT