Skip to main content

JAVA : W1-M2



Classes

Learning Objective

Welcome to the Task2 in Java. In this Module let us solve some more problems in Java Classes.

Make Sure that you have learnt following concepts covered in this module
  • What is Class
  • Object
  • Members
  • Methods
  • Constructor
  • Method Overloading
  • Constructor Overloading

Orientation Video




Introduction

Class:
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint describes the state and behavior that the created objects all share. An object created by a class is an instance of the class, and the class that created that instance can be considered as the type of that object, e.g. a type of an object created by a "Fruit" class would be "Fruit". A class may represent a person, place, or thing - it is an abstraction of a concept within a computer program.
Object:
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Overloading methods:
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance"). Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list. A method is a group of instructions that is given a name and can be called up at any point in a program simply by quoting that name.
Method:
A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invokewith the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur
In Java, Objects are passed by reference, and primitives are passed by value. This is half incorrect. Everyone can easily agree that primitives are passed by value; there's no such thing in Java as a pointer/reference to a primitive. However, Objects are not passed by reference. A correct statement would be Object references are passed by value. This may seem like splitting hairs, bit it is far from it. There is a world of difference in meaning. The following examples should help make the distinction.
In Java, take the case of
(Java) Pass-by-value example
public void foo(Dog d) {
  d = new Dog("Fifi"); // creating the "Fifi" dog
}
Dog aDog = new Dog("Max"); // creating the "Max" dog
// at this point, aDog points to the "Max" dog
foo(aDog);
// aDog still points to the "Max" dog
the variable passed in (aDog) is not modified! After calling foo, aDog still points to the "Max" Dog!
Many people mistakenly think/state that something like
(Java) Still pass-by-value...
public void foo(Dog d) {
  d.setName("Fifi");
}
shows that Java does in fact pass objects by reference.
The mistake they make is in the definition of
(Java) Defining a Dog pointer
Dog d;
itself. When you write that definition, you are defining a pointer to a Dog object, not a Dog object itself.
Constructor:
A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
For example, Bicycle has one constructor:

public Bicycle(int startCadence, int startSpeed, int startGear) {

       gear = startGear;
 cadence = startCadence;
 speed = startSpeed;
}
To create a new Bicycle object called myBike, a constructor is called by the new operator: Bicycle myBike = new Bicycle(30, 0, 8);
Constructor Overloading:
Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor. Whenever we assign the name of the method same as class name. Remember this method should not have any return type. This is called as constructor overloading.

Resources

http://java.sun.com/docs/books/tutorial/
http://en.wikipedia.org/wiki/Java

Programs for your reference
// Circle.java:  Contains both Circle class and its user class

public class MyMain
{
       public static void main(String args[])
       {
               Circle aCircle;  // creating reference
    aCircle = new Circle(); // creating object
     aCircle.r = 5;     // assigning value to data field
              
    double area = aCircle.area(); // invoking method
               double circumf = aCircle.circumference();
               System.out.println("Radius="+aCircle.r+" Area="+area);
               System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
       }
}

class Circle {

    public double r;    // radius of circle
       Circle() {  }           // no args constructor
     //Methods to return circumference and area
     public double circumference() {
  return 2*3.14*r;
     }
     public double area() {
  return 3.14 * r * r;
    }
}


Task1 Description :

  1. Write a Java Program which does the following:
    1. . Create a Class called " MSITStudent"
    2. . Members / Variables of the Class should be StudentName, StudentNumber, whichMiniSem, isEligible and isRegistered
    3. . Define 2 functions Registration and Eligibility. The Program has to tell whether the student can register for a course or not.
    4. . This depends on the Eligibility function. The Eligibility function should use a random function to say whether the student is eligible for a course or not. This variable has to be passed to Registration function where in which it returns a boolean variable based on the value sent by Eligibility function.
    5. . If Eligibility returns 0, (If the student is not eligible then registration cannot take place) Else Registration happens.
Note:
  • Use Random function to generate the value for Eligibility.
  • By default when a student object is created Is eligible and Is registered values are unknown for which value 2 can be taken.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

Task2 Description :

  1. Now that you have created a MSITStudent class, let us add some more flavor to it and learn more concepts. MSITStudents are required to register the courses at the beginning of the year. Year consists of 6 minis and each Mini has 1 course and a mentor for it.
    1. Create a Class called " MSITStudent"
    2. Create a Class called " Mentor"
    3. Members / Variables of the MSITStudent would be StudentName, StudentNumber, whichMiniSem, isEligible and isRegistered
    4. Members / Variables of the Class Mentor would be MentorName, Subject Offered, whichMiniSem [There are no methods declared for mentor]
    5. Now Instantiate array of objects in each class and your program should display the details of mentor and students belonging to a particular Mini.
      1. Instantiate 10 students for student class, 4 mentors for mentors class.
      2. Input the details of the students and mentors through keyboard.
      3. Tune your program to display the details of the mentor and students that belong to particular Mini.
Note:
  • The Eligibility and the registration functions are required here too. For which you can use your above class that you created in the Task1.
TRY TO COMPLETE LEARNING CONCEPTS AND COMPLETION OF TASKS WITH IN 4 HOURS

Comments