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

127 publie votd c2) Lab Activitios: Student Class In this lab, you will ) wribe

ID: 3589867 • Letter: 1

Question

127 publie votd c2) Lab Activitios: Student Class In this lab, you will ) wribe in Jaa a class that represents a model of a L. Draw the U Il. Write a Java source code for a class named Student. Use the Java kes I(I.) to refer to the referenced object, and (2.) to call a typical student and (2) a test class to test it. class diagram for your design (relating the 2 classes below). sure all the data field members (data fields) areprivate, 1. The class Student represents a typical student (in the form of keyword this in its TWO forns of usage from another constructor) everywhere you can. Make s student ' a lava objecp, Wite the stde s briefly described below A university student object, an instance (object) of Student class, has the state (expressed via its private data felds) can be defined as the student's name, address, mojor, and GPA , and via its methods) is to update the address and to recalculate the GPA behavior ( (ex address, major are EACH of String data type, the GPA data field is a double (1) The Student data fields name, data type. They must be declared private Use the Java this keyword to call one of the coestructors from the other. or takes a name, eddress, and mgjor. It also shouald have a call to comput eGPA.I. methold (2) Write hwo constructors. (see below how) to set the GP takes as formal parameters a name and address. Use this Java keyword to call When you call the first constructor with this, for the major argument, use the String the first constructor. value "Undeclared" set methods (also called mutators, setters) for the data fields/ instance variables: name, address, and ie, do not implement a set method for the GPA data field [since it is calculated/set by the computeGPA () method] 59) Implement get methods (also called accessons, getters) for ell of the instance variables (ie, incl. one for th Xa Write the computeGPA 0 void method. In it, to calculate the GPA, generate and return a random dou between 0.5 and 4.0. [Here, use the random 0 method of the Math class (import it in the beginning of t GPA). A program with the import java. lang.Math directive)] 7) Include a tostring () method to return the output for each student in the following format (notice,this is an me: Jeremy George iress: 1598 See Dr. or: Computer Science : 3.45 When you prepare the format of the GPA number, make sure that you have only 2 digits a decimal point (as in the above example: 3.45) for that purpose, to achieve it, make sure that y insde the tostring() method, instead of only the GPA data field, use the fellovsing for method. format

Explanation / Answer

import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;

class Student{

    private String name;
    private String address;
    private String major;
    private double GPA;
    public Student(String nm, String addr,String mj){
        name = nm;
        address = addr;
        major = mj;
        computeGPA();
    }
    public Student(String nm, String addr){
       this(nm,addr,"Undeclared");
    }
    public void computeGPA(){
        Random rand = new Random();
        GPA = 0.5 + (rand.nextDouble() * (4.0-0.5));
    }
    public void setName(String nm){
         name = nm;
    }
    public void setAddress(String ad){
         address = ad;
    }
    public void setMajor(String mj){
         major = mj;
    }
    public String getName(){
         return name;
    }
    public String getAddress(){
         return address;
    }
    public String getMajor(){
         return major;
    }
    public String getGPA(){
         DecimalFormat df = new DecimalFormat("#.00");
         String str;
         if (GPA < 1)
            str = "0" + df.format(GPA);
         else
            str = df.format(GPA);
         return str;
    }

    public String toString(){
         DecimalFormat df = new DecimalFormat("#.00");
         String str;
         if (GPA < 1)
            str = "0" + df.format(GPA);
         else
            str = df.format(GPA);
         return "Name:" + name + " " + "Address:" + address + " " + "Major:" + major + " " + "GPA:" + str + " ";
    }
   
}

public class DemoStudent{

    public static void main(String[] args){
          Student st = new Student("Jermy George","1598 Sec Dr. 1","Computer Science");
          Student st1 = new Student("John","Address 1");
          System.out.println(st.toString());   
          System.out.println(st1.toString());
               
    }  
}

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