Question

In: Computer Science

Design a class named Student that contains the followingprivate instance variables: A string data field name...

Design a class named Student that contains the followingprivate instance variables:

  • A string data field name for the student name.

  • An integer data field id for the student id.

  • A double data field GPA for the student GPA.

  • An integer data field registeredCredits.

    It contains the following methods:

  • An empty default constructor.

  • A constructor that creates a student record with a specified id and

    name.

  • The get and set methods for id, name, GPA, and

    registeredCredits.

  • A method called registerCredit that adds specified amount of

    credits to the student.

  • A method called withdrawCredit that withdraws specified

    amount of credits from the student.

  1. Implement the class.

  2. Write a test program that creates a student account using your

    name and id. Set all the variables information to your student information (GPA, and registered credits). Use the registerCreditmethod to add 9 more credits. Use the method withdrawCreditto withdraw 3 credits. Then prints out all the student information:name, id, GPA, and registerdCredits.

    Sample Output:
    Enter your GPA: 3.9
    Enter your registered credits: 30Student name: Sara
    Student ID: 12345678
    GPA: 3.9
    Registered credits: 36

    (( in java ))

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


Student.java

class Student
{
private String name;
private int id;
private double gpa;
private int registeredCredits;

// Constructor
Student(){
}
public Student(String name, int id) {
this.name = name;
this.id = id;
}

//Setter and getter
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public double getGpa() {
return gpa;
}

public void setGpa(double gpa) {
this.gpa = gpa;
}

public int getRegisteredCredits() {
return registeredCredits;
}

public void setRegisteredCredits(int registeredCredits) {
this.registeredCredits = registeredCredits;
}
  
//methods
public void registerCredit(double amount)
{
registeredCredits += amount;
}

public void withdrawCredit(double amount)
{
registeredCredits -= amount;
}
}


StudentTest.java

import java.util.Scanner;

public class StudentTest
{
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);

        // create object
        Student student = new Student("Sara", 12345678);

        //read GPA
        System.out.print("Enter Your GPA: ");
        double gpa= scanner.nextDouble();
        student.setGpa(gpa);

        //read Credits
        System.out.print("Enter Your registered credits: ");
        int credits= scanner.nextInt();
        student.setRegisteredCredits(credits);

        // add credits
        student.registerCredit(9);

        //remove credits
        student.withdrawCredit(3);

        //print data
        System.out.println("Student name: "+student.getName());
        System.out.println("Student ID: "+student.getId());
        System.out.println("GPA: "+student.getGpa());
        System.out.println("Registered credits: "+student.getRegisteredCredits());


    }
}

=================================


Related Solutions

Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT