Write java script for the following questionnaire from Zybooks. Note: Since all
ID: 3752866 • Letter: W
Question
Write java script for the following questionnaire from Zybooks.
Note: Since all the programs run on Zybooks compiler use tha name for each program as Main.
Ex: public class Main {
1. Create a class called "Student". Declare two private member fields for a student name (String) and GPA (double). Provide a constructor, get methods and set methods as shown below.
public Student() - set name to "Louie" and GPA to 1.0
public void setName(String n) - set the name
public String getName() - return the name
public void setGPA(double g) - set the GPA
public double getGPA() - return the GPA
2. Write a main method to prompt the user for an integer that represents the number of large pizzas to be ordered ($9.99 each). Calculate and display the sub-total before sales tax. Calculate and display the total due by applying a sales tax of 6%. Use the NumberFormat to display currency.
Sample Output
How many pizzas? 3
Sub Total: $29.97
Total Due: $31.77
3. Write a main method to prompt the user for two integers. Use the Random class to generate three numbers all within the range of low to high (inclusive).
Sample Output
Enter low: 15
Enter high: 20
Random values: 20 19 15
4. Write a main method to prompt the user for three numbers (doubles) that represent the side lengths of a triangle. Calculate the area of the triangle using Math.sqrt(). Print the area to three decimal places using DecimalFormat.
s = half the triangle's perimeter
area = the square root of s(s-a)(s-b)(s-c)
Sample Output
Enter Side A: 3
Enter Side B: 3
Enter Side C: 1
The area of the triangle is: 1.479
5. Write a main method that prompts the user for a first name, a last name and a four-digit integer (use nextInt()) on separate lines. Generate a login name that includes the first five letters of the last name, followed by the first letter of the first name and then the last two digits of the number (use the % operator).
Enter first name: Jeremy
Enter last name: Johnson
Enter 4-digts: 6789
Your login name: JohnsJ89
6. Write a main method that prompts the user for two doubles that are the radius and height of a cylinder. Calculate the volume and the area, as given by following two equations, using Math.pow() and Math.PI and print the results. Print the results to one decimal place using DecimalFormat.
Volume = r2h
Area = 2rh
Sample Output
Enter Radius: 5.2
Enter Height: 8.1
Volume: 688.1 cubic inches.
Surface Area: 264.6 square inches.
Explanation / Answer
1.
class Student{
private String name;
private double GPA;
public Student(){
name="Louie";
GPA = 1.0;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public void setGPA(double g){
GPA = g;
}
public double getGPA(){
return GPA;
}
}
2.
import java.util.*;
import java.text.*;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
NumberFormat nf = NumberFormat.getInstance(Locale.US);
System.out.print("How many pizzas? ");
int numPizzas = scanner.nextInt();
double subTotal = numPizzas*9.99;
double Total = subTotal+subTotal*0.06;
System.out.println("Sub Total: $"+nf.format(subTotal));
System.out.println("Total Due: $"+nf.format(Total));
}
}
3.
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter low: ");
int low = scanner.nextInt();
System.out.print("Enter high: ");
int high = scanner.nextInt();
int a,b,c;
a = low + (int)(Math.random()*(high-low)+1);
b = low + (int)(Math.random()*(high-low)+1);
c = low + (int)(Math.random()*(high-low)+1);
System.out.println("Random values: "+a+" "+b+" "+c);
}
}
4.
import java.util.*;
import java.text.DecimalFormat;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DecimalFormat ft = new DecimalFormat("#.###");
System.out.print("Enter Side A: ");
int a = scanner.nextInt();
System.out.print("Enter Side B: ");
int b = scanner.nextInt();
System.out.print("Enter Side C: ");
int c = scanner.nextInt();
double s = (a+b+c)/2.0;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of triangle is: "+ft.format(area));
}
}
5.
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first name: ");
String firstName = scanner.next();
System.out.print("Enter last name: ");
String lastName = scanner.next();
System.out.print("Enter 4-digits: ");
int num = scanner.nextInt();
int two = num%10;
num = num/10;
two = two + num%10*10;
String login = lastName.substring(0,5)+firstName.charAt(0)+two;
System.out.println("Your login name: "+login);
}
}
6.
import java.util.*;
import java.text.DecimalFormat;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Radius: ");
double radius = scanner.nextDouble();
System.out.print("Enter Height: ");
double height = scanner.nextDouble();
double volume = Math.PI*Math.pow(radius,2.0)*height;
double area = 2*Math.PI*radius*height;
DecimalFormat df = new DecimalFormat("#.#");
System.out.println("Volume: "+df.format(volume)+" cubic inches");
System.out.println("Surface Area: "+df.format(area)+" cubic inches");
}
}
//any query, post in the comment section
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.