OOP – Assignment VII

Lab Manual Download : Write a program in Java with class Rectangle with the data fields width, length, area and colour. The length, width and area are of double type and colour is of string type. The methods are get_length(), get_width(), get_colour() and find_area(). Create two objects of Rectangle and compare their area and colour. If the area and colour both are the same for the objects then display “ Matching Rectangles”, otherwise display “ Non-matching Rectangle”.

ASSIGNMENT NO 7

 Title: Demonstrate use of class and methods.

Objectives: To learn concept of classes, methods, objects and use of built in functions in Java.

Problem Statement:  Write a program in Java with class Rectangle with the data fields width, length, area and colour. The length, width and area are of double type and colour is of string type. The methods are get_length(), get_width(), get_colour() and find_area(). Create two objects of Rectangle and compare their area and colour. If the area and colour both are the same for the objects then display “ Matching Rectangles”, otherwise display “ Non-matching Rectangle”.

Outcomes: Students will be able to demonstrate use of variables, data types, operators and expressions and switch statement in Java.

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more. 

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

 Theory:

Object in Java : An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of intangible object is banking system. An object has three characteristics:

  1. state:represents data (value) of an object.
  2. behavior:represents the behavior (functionality) of an object such as deposit, withdraw etc.
  3. identity:Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.

For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.

 Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

 Object Definitions:

  • Object isa real world entity.
  • Object isa run time entity.
  • Object isan entity which has state and behavior.
  • Object isan instance of a class.

Class in Java : A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can’t be physical. A class in Java can contain:

  1. fields
  2. methods
  3. constructors
  4. blocks
  5. nested class and interface

 Syntax to declare a class:

class <class_name>{  
    field;  
    method;  
}

Instance variable in Java: A variable which is created inside the class but outside the method, is known as instance variable. Instance variable doesn’t get memory at compile time. It gets memory at run time when object(instance) is created. That is why, it is known as instance variable.

 Method in Java : A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.

Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design.

 Creating Method : Considering the following example to explain the syntax of a method −

Syntax

public static int methodName(int a, int b) {
   // body
}

Here,

public static − modifier

int − return type

methodName − name of the method

a, b − formal parameters

int a, int b − list of parameters

Method definition consists of a method header and a method body. The same is shown in the following syntax −

 Syntax

modifier returnType nameOfMethod (Parameter List) {
   // method body
}

The syntax shown above includes −

  1. modifier− It defines the access type of the method and it is optional to use.
  2. returnType− Method may return a value.
  3. nameOfMethod− This is the method name. The method signature consists of the method name and the parameter list.
  4. Parameter List− The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
  5. method body− The method body defines what the method does with the statements.

 Example

Here is the source code of the above defined method called max(). This method takes two parameters num1 and num2 and returns the maximum between the two −

/** the snippet returns the minimum between two numbers */
public static int minFunction(int n1, int n2) {
   int min;
   if (n1 > n2)
      min = n2;
   else
      min = n1;
   return min;
}

Method Calling : For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value).

The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when −

  1. the return statement is executed.
  2. it reaches the method ending closing brace.

The methods returning void is considered as call to a statement. Lets consider an example

System.out.println(“UNDERDSTAND!!! IMPLEMENT!!! ANALYZE!!!”);

The method returning value can be understood by the following example −

int result = sum(6, 9);

Following is the example to demonstrate how to define a method and how to call it −

Example

public class ExampleMinNumber {
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      int c = minFunction(a, b);
      System.out.println("Minimum Value = " + c);
   }
   /** returns the minimum of two numbers */
   public static int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;
     return min;
  }
}

This will produce the following result −

 Minimum value = 6

new keyword in Java : The new keyword is used to allocate memory at run time. All objects get memory in Heap memory area.

Conclusion : In this assignment, we have studied classes and methods in Java and demonstrated their use in program.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty
             

Program

class Rectangle
{
 double len,br; //variable for length, breadth and color
 String color;
 double area;
 void read() //function to enter values from keyboard/user
 {
 System.out.println("Enter Length and Width of Rectangle");
 len=Double.parseDouble(System.console().readLine());
 br=Double.parseDouble(System.console().readLine());
 System.out.println("Enter Color of Rectangle");
 color=System.console().readLine();
 }
 void display()
 {
 System.out.println("Length and Width of Rectangle : "+len+","+br);
 System.out.println("Color of Rectangle"+color); 
 System.out.println("Area of Rectangle is"+area);
 }
 double findArea()
 {
 area= len*br;
 return area;
 }
 String getColor()
 {
 return color;
 }
 public static void main(String args[])
 {
 Rectangle r1,r2;
 r1=new Rectangle();
 r2=new Rectangle();
 System.out.println("********First Rectangle************");
 r1.read();
 System.out.println("************Second Rectangle************");
 r2.read();
 System.out.println("********First Rectangle************");
 r1.display();
 System.out.println("************Second Rectangle************");
 r2.display();
 double a1=r1.findArea();
 double a2=r2.findArea();
 String c1=r1.getColor();
 String c2=r2.getColor();
 if((a1==a2) && (c1.equalsIgnoreCase(c2)))
 System.out.println("Rectangles are matching");
 else
 System.out.println("Rectangles are not matching");
}
}

 

OUTPUT:
ass-iii-opass-iii-op2

Leave a Reply

Your email address will not be published. Required fields are marked *