100% found this document useful (1 vote)
288 views6 pages

Batch78 Practicals3

The document provides examples and exercises to practice object-oriented programming concepts in Java, including: 1. Testing methods in the Math class. 2. Generating random numbers. 3. Creating a Duck class with attributes and behaviors. 4. Creating a Person class to demonstrate conditionals based on attributes. The exercises continue exploring classes, objects, parameters, constructors, access modifiers, and more challenging concepts like state and behavior.

Uploaded by

Mallikarjun Mbr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
288 views6 pages

Batch78 Practicals3

The document provides examples and exercises to practice object-oriented programming concepts in Java, including: 1. Testing methods in the Math class. 2. Generating random numbers. 3. Creating a Duck class with attributes and behaviors. 4. Creating a Person class to demonstrate conditionals based on attributes. The exercises continue exploring classes, objects, parameters, constructors, access modifiers, and more challenging concepts like state and behavior.

Uploaded by

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

!

Uttara InfoSolutions
www.uttarainfo.com

Uttara Practicals 3!

!
1) Write a program to test working of Math class. Test the following
methods (see TestMath.java for example):!
double result;!
result = Math.sqrt(double value); // returns square root of value!
result = Math.cbrt(double value); // returns cube root of value!
result = Math.pow(double value1, double value2); // returns power!
result = Math.floor(9.999); // take it to the floor - reduce to lower decimal!
result = Math.ceil(9.009); // take it to the ceiling - to the higher decimal!
result = Math.round(9.5);!
result = Math.round(9.49);!
result = Math.abs(+9.999);!
result = Math.abs(-9.999);!
result = Math.random(); // returns a random double value!
// SOP(result) each time you call the method to test.!

2) Print 10 random values between 0-100 (call Math.random() 10 times


in a for loop and print result to monitor). Now print 10 random integer
values between 0-100 (simply multiply 100*Math.random() and then
downcast to int like this -> int val = (int) (100*Math.random()); SOP(val);
Do you understand what is happening? Can you print 10 random values
between 0 - 1000 now?!

3) Create a Duck. A Duck has a tailSize (int). A Duck can swim. When!
you ask a Duck to swim, it says so (print to monitor). Design and test!
Duck class usage as we did for Hippo.!
!
Duck!
!
!
int tailSize;!
!
!
public void swim()!
!
!
{!
!
!
!
System.out.println(Duck swimmingswimming);!
!
!
}!
Do you understand why tailSize is an instance variable?!
Do you understand why swim() is non-static?!
Do you understand what happens when an object is created by user by!
JVM?!

4) There are Persons. A person has a age, name. Persons can eat,
sleep. A person sleeps more if his age is < 40 and eats less. If the
persons age is >= 40, he sleeps less but eats more. Design a Person
class and test it.
Person
!
String name;

int age;
!
public void eat() {


if(age < 40) !
!
!
!
sdfsdkf


else
!
!
!
adfkgldafgk

}
!
public void sleep() { !
!
!
.

}!

5) Test passing reference as parameter. Create a Fish class with String


name in it. Create a Croc class with String name and a method called
eat().Accept a Fish reference as parameter. In the eat(), print both the
crocs name and fish`s name (public void eat(Fish f)). In TestPass class,
code a main() where you should create a croc object, a fish object,
assign names cocky and fishy and invoke eat() and pass fish ref as
parameter.Print the fish`s name before calling eat() and after calling
eat(). Run and test.!

Test the following as well:!

a) in eat(), after SOP, assign f to null. Check what happens.!


b) in eat(), assign f to new Fish object and assign name Flippy. Now
check the SOPs!
c) in eat(), assign f.name to Flippy and observe outputs. What happens
here? How are references getting passed?!
d) pass null to eat() and check what happens!

6) There are Dogs. Every Dog has a name and a size. Dogs can bark. If
the size of the dog is > 10, it "meows". If the size <=10, then as many
times, it "bow wow" its name to the monitor. Test Dog design. After
testing the same, make the size variable private and then add setSize()/
getSize() method. See how this impacts your tester class. What check
should you add in bark() to ensure that even if the class user has not set
size and invokes bark, he gets scolded with a message?!
7) Create a class Song. A song has a name (String) and lyrics (String). A
song can be played. When you play, it prints out its lyrics to monitor.
Create the class with 2 instance variables with 2 setter / getter methods
(setName(String str), setLyrics(String n), getName(), getLyrics(). Make
sure validate for null and empty string). Now create a TestSong class
with main(). Create 2 objects of Song and set different names and lyrics.
Invoke play() and verify if the songs are playing correctly. Now invoke
setter methods to change the lyrics and invoke play() again. Has the
lyrics changed or not?!
Now create a paramterized constructor to accept both name and lyrics
in Song class. Put an SOP in the constructor as well. Compile
Song.java. Try to recompile TestSong.java. Is the compilation
succeeding? Why not? Change your code in TestSong to create 2 song
objects by passing parameters to constructors. Comment out the setter
methods used. Now recompile and run the program. Do you understand
the usage of Constructors now? Add another constructor to Song to be
accept only name as parameter. Can we have multiple constructor in the
same class? Put SOP in this constructor too. In TestSong, create
another Song object by passing only name. Check by calling play() as to
what is the lyrics being printed. Why so? Now invoke setLyrics() and
pass a lyrics string as param. Invoke play() again on the same object.

Do you now understand why to have setter methods still in a class that
contains constructors? Make sure you validate in constructors too.!

8) Create a class X. Create an int field a and assign 10 to it.!


Create an instance initializer and put in SOP("in inst init 1 a = +a); and
change a value to 20.!
Create another instance initializer and put in SOP("in inst init 2 a = +a);
and change a value to 30.!
Create a no-arg constructor, print value of a and then assign 40 to it.!
In a Tester class, in main()-> create object of X and then print obj.a!
to the monitor. Do you understand how the initialisation is happening?!
Now add a static variable in X named b = 15. Create 2 static init where
you print the value of b and change them like earlier. Add printing of X.b
in Tester class and then run it. Do you understand now how the
initialisation is occurring. Create one more object of X and see if the
static initializers are getting fired.!

!
See this below code only after following the instructions above.!
!
X.java->!
!
public class X!
{!
!
int a = 10;!
!
static int b = 15;!
!
static!
!
{!
!
!
System.out.println("in static init 1 b = "+b);!
!
!
b = 25;!
!
}!
!
{!
!
!
System.out.println("in inst init 1 a = "+a);!
!
!
a = 20;!
!
}!
!
{!
!
!
System.out.println("in inst init 2 a = "+a);!
!
!
a = 30;!
!
}!
!
static // does order matter for init execution?!
!
{!

!
!
System.out.println("in static init 2 b = "+b);!
!
!
b = 35;!
!
}!
!
public X()!
!
{!
!
!
System.out.println("in constr of X a = "+a);!
!
!
a = 40;!
!
}!
}!
9) *Important problem* Cars can be started, driven, reversed or stopped.
You have to start the car to drive/reverse/stop it. When car is being
driven / reversed, the fuel reduces. Once the car has no fuel, the car
stops. Every car has a name. Write a tester class to test cars. How to
know whether the car has started and then only being asked to be
driven? Try to design on paper first and then see the below design.
Understand how state can impact behaviour.!
Car!
!
!
!
!
!
!
!

- name!
- fuelQty!
- isStarted : boolean!
- start()!
!
// if fuelQty > 0!
!
//!
set isStarted = true;!
- drive()!

!
!
!
!
!
!
!

!
!
!
!
!
!
!

!
!
!

- reverse()!
- stop()!
!
// isStarted = false!!

!
!

// check if the car has started, only if !


yes,!
if(isStarted)!
!
// check if fuelQty>0, then only drive!
!
// reduce fuelQty by 1 unit!
!
// if fuelQty==0!
!
!
stop();!

10) Write a method to compare 2 persons (coded in problem 5) based


on age.!

public class PersonComparator!


{!
!
public static int comparePersons(Person p1, Person p2)!
!
{!
!
!
//impl code!
!
}!
}!
Return -ve if age of first person is less than age of second!
Return +ve if age of first person is greater than second!
Return 0 if age of both are the same. !
In main(), create 4 person objects and then using the
PersonComparator, compare them and print who is elder to whom.

You might also like