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

Javascript validation, Javascript validation, JAVA Programming 1. Obtaining...

Javascript validation, JAVA Programming 1. Obtaining the new script: Download and save the attached comment CGI mailer script form-mail2.pl to your server''s cgi-bin directory, and

Explain the role of remote interface in rmi, The Remote interface serves to...

The Remote interface serves to recognize interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly ex

Banking database system - java database connectivity, JDBC Assignment B...

JDBC Assignment Banking Database System:  BankCustomer CUST_ID  CUST_NAME ADDRESS ACC_TYPE CUST_ID

What is bandwidth, What is bandwidth? In a general way Bandwidth is a c...

What is bandwidth? In a general way Bandwidth is a capacity of communication channel of carrying data.

Smugglers decodes , Smugglers are becoming very smart day by day. Now they ...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

What is overriding, What is Overriding? When a class explains a method ...

What is Overriding? When a class explains a method using the similar name, return type, and arguments as a method in its superclass, the method in the class overrides the metho

Error, UnsupportedClassVersionError

UnsupportedClassVersionError

Area Under Curve, #include float start_point, /* GLOB...

#include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; int numtraps; main( ) { void input( void );

How many iterations would this for loop run for, How many iterations would ...

How many iterations would this ‘for' loop run for? for ( x = 50 ; x document.write ( x ) ; } Answer: 50 iterations

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