Question

In: Computer Science

Code in Java Write a Student class which has two instance variables, ID and name. This...

Code in Java

Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set, print a relevant message stating value could not be changed.

Finally, code a main method that will create an instance of Student class with ID as “5678654” and name as “Shawn” then print the ID and name to screen. Then use the setters to set the value of ID to “123456789” and name to “JohnCarlo” and then print the values of ID and name by using their getters.

Solutions

Expert Solution

Here is the complete java code:

public class Student {
    // Making id as int and name as string
    private int ID;
    private String name;

    // Here is the constructor with id and name as parameters
    Student(int id, String n) {
        ID = id;
        name = n;
    }

    // gettter for id
    int getId() {
        return ID;
    }

    // getter for name
    String getName() {
        return name;
    }

    // setter for id
    void setID(int newId) {

        // Here we convert int to string so we can calculate the length and verify that
        // it is between 6 and 8
        if (String.valueOf(newId).length() < 6 || String.valueOf(newId).length() > 8) {
            // if it is not then we print the message and return from the function
            System.out.println("The length should be between 6-8 for ID");
            return;
        }
        ID = newId;
    }

    // setter for name
    void setName(String newname) {
        // if length is not between 0 and 20 then we show message and return
        if (newname.length() <= 0 || newname.length() > 20) {
            System.out.println("The length should be between 0-20 for name");
            return;
        }
        name = newname;
    }

    // This is the main function
    public static void main(String[] args) {
        Student obj = new Student(5678654, "Shawn");
        System.out.println("The ID: " + obj.getId() + " The Name: " + obj.getName());
        // The next line will show the message that length should be between 6 and 8
        obj.setID(123456789);
        obj.setName("JohnCarlo");
        System.out.println("\nThe ID: " + obj.getId() + " The Name: " + obj.getName());
    }
}

Here is the output:

it is observed that the id remains unchanged


Related Solutions

In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
A Java question. You are given a Student class. A Student has a name and an...
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a class named Classroom which manages Student objects. You will provide the following: 1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID...
JAVA CODE PLEASE write a casino membership application. in the casino membership class: data: membership ID points method: add points display points Main: create a membership object (a client) give initial points (600) increase the points by (try both 200 and 500)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT