Batch78 Practicals3
Batch78 Practicals3
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.!
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() { !
!
!
.
}!
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.!
!
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!!
!
!