Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the question below, use the following class definition. import java.text.Dec

ID: 3779091 • Letter: F

Question

For the question below, use the following class definition. import java.text.DecimalFormat; public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced return name + " " + major + " " + df.format(gpa) + " " + hours } } Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in typical form (like 3.810)? 1. "0.# " 2. "0.0##" 3. "#.0" 4. "0.000" 5. "#.###"

Explanation / Answer

The Correct option is 4. "0.000"

----------------------------------------------------------------------------------------------------

/**
* The Student that sets the name, major,gpa and
* hours.
* */
//Student.java
import java.text.DecimalFormat;
public class Student
{   
   //declare instance variables
   private String name;
   private String major;
   private double gpa;
   private int hours;
   //Student constructor
   public Student(String newName, String newMajor, double newGPA, int newHours)
   {
       name = newName;
       major = newMajor;
       gpa = newGPA;
       hours = newHours;
   }
   public String toString( )
   {
       /*Note : # represents a digit
          0 denots a digit or 0 if number is not a digit
          So the correct option is 0.000
          So when prited 3.81 as 3.810 , "0.000" is set as argument
          for DecimalFormat class.*/
       DecimalFormat df = new DecimalFormat("0.000");

       return name + " " + major + " " + df.format(gpa) + " " + hours ;
   }
}//end of Student

//StudentTester.java
public class StudentTester
{
   public static void main(String[] args)
   {
       //Create an instance of Student class
       Student std=new Student("Johnson", "CS",3.810, 60);
       //print std object
       System.out.println(std);
   }//end of main method
}//end of class StudentTester


Sample output:

Johnson
CS
3.810
60

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote