0% found this document useful (0 votes)
21 views16 pages

Discussion HR 4 7th Dec

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

Discussion HR 4 7th Dec

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming

Topic: JAVA

Discussion Hr Dr. Sreedevi A. G


Asst. Prof. CSE
CSE AI
Amrita School of Engineering
07-Dec-2020; 01.50 pm | Chennai Campus
1
Contents
• Classes, Objects and
Constructors -
Programming
Exercises

2
Exercise 1
1.Write a program to print the names of
students by creating a Student class. If no
name is passed while creating an object of
Student class, then the name should be
"Unknown", otherwise the name should be
equal to the String value passed while
creating object of Student class.

3
class Student {
String name;
public Student(String s){
name = s;
}
public Student(){
name = "Unknown";
}
}

class Ans{
public static void main(String[] args){
Student s = new Student("xyz");
Student a = new Student();

System.out.println(s.name);
System.out.println(a.name);
}
}

4
Exercise II
1.Write a program to print the area and
perimeter of a triangle having sides of 3, 4
and 5 units by creating a class named
'Triangle' with constructor having the three
sides as its parameters.

5
class Triangle{
int a,b,c;
public double getArea(){
double s = (a+b+c)/2.0;
return Math.pow((s*(s-a)*(s-b)*(s-
c)),.5);
}
public double getPerimeter(){
return (a+b+c)/2.0;
}
}

class Ans{
public static void main(String[] args){
Triangle t = new Triangle();
t.a = 2;
t.b = 5;
t.c = 6;
System.out.println(t.getArea());
System.out.println(t.getPerimeter());
}
} 6
Exercise III
1.Write a program to print the area of two
rectangles having sides (4,5) and (5,8)
respectively by creating a class named
'Rectangle' with a method named 'Area'
which returns the area and length and
breadth passed as parameters to its
constructor.

7
class Rectangle{
int length;
int breadth;
public Rectangle(int l, int b){
length = l;
breadth = b;
}
public int getArea(){
return length*breadth;
}
public int getPerimeter(){
return 2*(length+breadth);
}
}

class Ans{
public static void main(String[] args){
Rectangle a = new Rectangle(4,5);
Rectangle b = new Rectangle(5,8);
System.out.println("Area : "+a.getArea()+" Perimeter is
"+a.getPerimeter());
System.out.println("Area : "+b.getArea()+" Perimeter is
"+b.getPerimeter());
8
}
Exercise IV
1.Write a program to print the area of a
rectangle by creating a class named 'Area'
having two methods. First method named
as 'setDim' takes length and breadth of
rectangle as parameters and the second
method named as 'getArea' returns the
area of the rectangle. Length and breadth
of rectangle are entered through keyboard.
9
Exercise V
1.Write a program to print the area of a
rectangle by creating a class named 'Area'
taking the values of its length and breadth
as parameters of its constructor and
having a method named 'returnArea' which
returns the area of the rectangle. Length
and breadth of rectangle are entered
through keyboard.
10
import java.util.*;
class Area{ System.out.println("Enter length");
int length; l = s.nextInt();
int breadth; System.out.println("Enter breadth");
public Area(int l, int b){ b = s.nextInt();
length = l;
breadth = b; Area a = new Area(l,b);
} System.out.println("Area :
public int getArea(){ "+a.getArea());
return length*breadth; }
} }
}

class Ans{
public static void main(String[]
args){
Scanner s = new Scanner(System.in);
int l,b;

11
Exercise VI
1.Print the average of three numbers
entered by user by creating a class named
'Average' having a method to calculate and
print the average.

12
Exercise VII
1.Write a program by creating an 'Employee'
class having the following methods and
print the final salary.
• 'getInfo()' which takes the salary, number of hours of
work per day of employee as parameter
• 'AddSal()' which adds $10 to salary of the employee if
it is less than $500.
• 'AddWork()' which adds $5 to salary of employee if
the number of hours of work per day is more than 6
hours.
13
Exercise VIII
1.Create a class called 'Matrix' containing
constructor that initializes the number of
rows and number of columns of a new
Matrix object. The Matrix class has the
following information:
• number of rows of matrix
• number of columns of matrix
• elements of matrix in the form of 2D array

14
Exercise IX
1.The Matrix class has methods for each of
the following:
• get the number of rows
• get the number of columns
• set the elements of the matrix at given position
(i,j)
• adding two matrices. If the matrices are not
addable, "Matrices cannot be added" will be
displayed.
• multiplying the two matrices
15
THANK YOU….

16

You might also like