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

Program with fibonacci sequence of first 20 terms, Program 1. Write a J...

Program 1. Write a Java program to display the first 20 Fibonacci numbers Program with Fibonacci sequence of first 20 terms Output 2. Write an applet progr

Create an online multiple choice quiz, Create an online multiple-choice qui...

Create an online multiple-choice quiz using JSP/Servlets technology. The quiz should draw questions from an array of predefined questions (at least fifteen). You should use a meani

Which models are supported by jms, Which models are supported by JMS? Pleas...

Which models are supported by JMS? Please, explain them. Ans) Publish or subscribe (pub/sub). This model permits a client (publisher) to send messages to a JMS topic. These mess

Area under curve., write a program to find the area under the curve y=f(x) ...

write a program to find the area under the curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b.   #include float start_point,

SWING, Application using Swing API

Application using Swing API

I want an idea for final year projects, i m confused what to take as a pro...

i m confused what to take as a project for final year in information tech cn u suggest some of the topic of software

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

Benefits of aop (aspect oriented programming), Benefits of AOP (Aspect Orie...

Benefits of AOP (Aspect Oriented programming) OOP can give the system level code like transaction management, logging, security etc to scatter throughout the business logic. A

What is inheritance in java explain with example, What is Inheritance in ja...

What is Inheritance in java Explain with example? Code reusability is claimed to be a key advantage of object-oriented languages over non-object-oriented languages. Inheritance

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