Illustrate an example to define a student class, JAVA Programming

Assignment Help:

Task - Defining a Student class

The below illustration will explain how to write a class. We want to write a "Student" class which

- should be able to store the below characteristics of student

- Roll No

- Name

- Provide default, parameterized and copy constructors

- Provide standard getters/setters (discuss shortly) for instance variables

- Make sure, roll no has never assigned a negative value i.e. ensuring correct state of object

- Provide print method capable of printing student object on console

Getters / Setters

Attributes of a class are usually taken as private or protected. So to access them outside of a class, a convention is followed knows  as  getters  &  setters.  These  are usually public  methods.  Words set and get are used prior to name  of  an attribute.  Another  significant purpose  for  writing  getter  & setters to control the values assigned to an attribute.

Student Class Code

// File Student.java

public class Student {

private String name;

private int rollNo;

// Standard Setters

public void setName (String name) {

this.name = name;

}

 

// Note the masking of class level variable rollNo

public void setRollNo (int rollNo) {

if (rollNo > 0) {

this.rollNo = rollNo;

}else {

this.rollNo = 100;

}

}

// Standard Getters

public String getName ( ) {

return name;

}

public int getRollNo ( ) {

return rollNo;

}

 

// Default Constructor public Student() {

name = "not set";

rollNo = 100;

}

 

// parameterized Constructor for a new student

public Student(String name, int rollNo) {

setName(name); //call to setter of name

setRollNo(rollNo); //call to setter of rollNo

}

 

// Copy Constructor for a new student

public Student(Student s) {

name = s.name;

rollNo = s.rollNo;

}

 

// method used to display method on console

 

public void print () {

System.out.print("Student name: " +name);

System.out.println(", roll no: " +rollNo);

}

} // end of class

 


Related Discussions:- Illustrate an example to define a student class

Moving test, After refactor the main class to become abstract class, I want...

After refactor the main class to become abstract class, I want to move test method from tset clas of main class to subclass, how to do that?

Android, What is basic of android?

What is basic of android?

Create an applet for grid layout, Take the Student Choice GUI you created i...

Take the Student Choice GUI you created in Assignment and convert it to an applet.  Because standalone GUI applications and applets are so similar, a lot of the code should be reus

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Build KNN Classifier in Java, Can you help? this assignment I will need to ...

Can you help? this assignment I will need to build a very straightforward and fully functional machine learning classifier with the K-Nearest Neighbor (KNN) algorithm. The KNN mode

Java developer with spring framework prerequisite, Java developer with spri...

Java developer with spring framework prerequisite from anywhere Let me know that you have proven experience in java with spring frame work. Send some sample data and provide inf

Online music store application, An online music store offers all songs for ...

An online music store offers all songs for 3$ each. The store requires members to prepay any amount of money they wish, and then download as many songs accordingly. You are require

We require responsible and serious programmer, We require responsible and s...

We require responsible and serious programmer Project Description: Big Data and On-line tools Website Liferay - JAVA - Portlets - JSP and JSF Skills required: J2EE,

43560 square feet., calculate the size of the tract of land by the size of ...

calculate the size of the tract of land by the size of an acre to get the number of acres.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd