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

Explain the execute phase of java program development, Explain the Execute ...

Explain the Execute phase of Java Program Development Finally in phase computer, under the control of its CPU, interprets program one bytecode at a time. Hence performing

Garbage collection guarantee, Is it right that garbage collection guarante...

Is it right that garbage collection guarantee that a program will not run out of memory?

Differentiation between an applet and an application, Differentiation betwe...

Differentiation between an Applet and an Application ?

Explain what is naming applets in java applets, Explain what is Naming Appl...

Explain what is Naming Applets in java applets? You can give an applet a name by using the NAME attribute of the APPLET element. This allows communication between different app

Padovan series, write a program in java using array and scanner class to ge...

write a program in java using array and scanner class to generate padovan series

Alreadyboundexception thrown and by which method, When is AlreadyBoundExcep...

When is AlreadyBoundException thrown and by which method? Ans) AlreadyBoundException is thrown by bind(String name) method when a remote object is already registered with the re

Need to develop chat application for j2me, Need to develop Chat application...

Need to develop Chat application for j2me and I want Image Processing code Project Description: We want a chat application for a community website. Image processing and co

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