In: Computer Science
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.
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!