213k views
0 votes
If possible can someone give me a hand with this, and how to do it in java.

Part 1 - The Student Class
Create a class called Student that includes the following instance variables:
lastName (type String)
firstName (type String)
id (type String)
totalCredits (type int)
activeStatus (type boolean)
Provide a constructor that initializes all of the instance variables and assumes that the values provided are correct.
Provide a set and a get method for each instance variable.
Create a method displayStudent that displays student information for the object in the following way:
Student ID: 12345
First Name: Tamara
Last Name: Jones
Total Credits: 34
Active: true
Note that this is sample data and should be replaced with your object's values.
Part 2 - The StudentTest Class
Creating a new StudentTest class. This is used to test out your student class.
Create a new Student object by instantiating (calling constructor) and passing in values for last name, first name, id, total credits and active status.
These values can be hard-coded into your program (i.e. not provided by the user).
Print out the student information by calling displayStudent
Ask the user for an updated value for "total credits" and update the object with this value by calling the appropriate set method.

1 Answer

4 votes

Sure, I can help you with that. Here's the Java code for both parts:

Part 1 - The Student Class

java

public class Student {

private String lastName;

private String firstName;

private String id;

private int totalCredits;

private boolean activeStatus;

public Student(String lastName, String firstName, String id, int totalCredits, boolean activeStatus) {

this.lastName = lastName;

this.firstName = firstName;

this.id = id;

this.totalCredits = totalCredits;

this.activeStatus = activeStatus;

}

// Getters and Setters

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public int getTotalCredits() {

return totalCredits;

}

public void setTotalCredits(int totalCredits) {

this.totalCredits = totalCredits;

}

public boolean isActiveStatus() {

return activeStatus;

}

public void setActiveStatus(boolean activeStatus) {

this.activeStatus = activeStatus;

}

// Display Method

public void displayStudent() {

System.out.println("Student ID: " + id);

System.out.println("First Name: " + firstName);

System.out.println("Last Name: " + lastName);

System.out.println("Total Credits: " + totalCredits);

System.out.println("Active: " + activeStatus);

}

}

Part 2 - The StudentTest Class

java

import java.util.Scanner;

public class StudentTest {

public static void main(String[] args) {

// Create a new Student object

Student student = new Student("Jones", "Tamara", "12345", 34, true);

// Display the student information

student.displayStudent();

// Ask for an updated value for total credits

Scanner scanner = new Scanner(System.in);

System.out.print("Enter updated total credits: ");

int updatedCredits = scanner.nextInt();

// Update the student object with the new value

student.setTotalCredits(updatedCredits);

// Display the updated student information

student.displayStudent();

}

}

In this program, we create a Student object and display its information using the displayStudent() method. Then, we ask the user to enter an updated value for totalCredits, which we set using the setTotalCredits() method, and then display the updated information again using displayStudent().

User Furqan Aziz
by
9.1k points