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

Networking, which concept is used to connect the networking in java

which concept is used to connect the networking in java

Minimumshelf, Write a program that finds the minimum total number of shelve...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process

Difference b/w deep cloning and shallow cloning of object, The default natu...

The default nature of an object's clone() function automatically provides a shallow copy. So to need a deep copy the classes have to be edited or adjusted.   Shallow co

How can you performance tune your database?, 1. De normalizes your tables w...

1. De normalizes your tables where required. 2. Proper use of index columns: An index based on numeric parts is more efficient than an index based on character columns. 3. Re

Enumerate the concept of write once run anywhere - java, Enumerate the conc...

Enumerate the concept of write once run anywhere - JAVA Java has introduced concept of WORA (write once run anywhere). When you write a java program it is called source code of

Explain pop, SMTP (Simple Mail Transfer Protocol) permits two mail servers ...

SMTP (Simple Mail Transfer Protocol) permits two mail servers to communicate using a easy language, and gives a step-by-step protocol for exchanging information. SMTP delivers m

Can a human doctor be replaced through an expert system, Can a human doctor...

Can a human doctor be replaced through an Expert System? Give reasons to support your answer. Expert System is a computer system that simulates the knowledge and expertise of a

Need java netbeans platform developer, Project Description: I want a lot...

Project Description: I want a lot of developers to develop modules The basic purpose of this project is to prepare a standard ERP Software Company will be divided into module

Implement design in the java programming language, Implement your design in...

Implement your design in the Java programming language. Your main program file must be called MIPSim.java. Your simulator should implement the MIPS instruction set with the rest

Brute force search, how to implement brute foce in a program?

how to implement brute foce in a program?

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