Questions on OOPs in Java
Not Knowing OOPs means not Knowing Java in this post I will list Some Questions based on OOPs which will Strengthen your knowledge of the OOPs Concept and these are the Concept that you can't afford to miss.
Questions on OOPs in Java:
1) 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. Create 2 Duck objects in tester class (TestDuck->main()), with 2 references pointing to it (lets say d1,d2). Set the tailSize states to 10 and 20. Invoke d1.swim() & d2.swim(). Compile, run and test your understanding so far. Now make d1.size = d2.size. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1.size = 30. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1=d2. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1=d2=null. Can you access any of the 2 objects now? Now do d1= new Duck(); How many objects are reachable? Do you understand the answers to all the questions? If not, please comment.
2) A Pen has inkQty (int), color (string) and can be used to write and refill. A text must be given for it to write. A quantity must be given to refill. If there is ink then the pen will write the text given to it (SOP). Refill works by taking in the int qty to add to the existing inkQty. First as a class designer, on paper apply OOAD and arrive at the class design. Then create the class implementation and create a tester class to create 2 pen objects, give it inkQty and ask it to write. First code this on your own. If you cannot get it correctly, then see Pen.java and TestPen.java. Then fix the code and rerun. Do not see the code first.
3) 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?
4) There are Accounts. Every Account has a number (string), owner (string) and balance (double). You can withdraw(double amt), debit(doubt amt), and checkBalance(). When you withdraw, the balance should reduce accordingly. When you debit, the balance will increase accordingly. Code an Account class and test it by creating 2 account objects? How do you stop over withdrawal?
5) There are TVs. A TV has a name and channel that is being displayed. You can increment/decrement channel. You can change the channel to a given number as well. You can ask the TV to display. When a TV is asked to display, it will print the channel num, the volume.TV has volume (int). You have to switch on the TV first before you can operate the channels or increase or decrease the volume. Design and test TV working.
The answer to Questions on OOPs in Java:
1) 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. Create 2 Duck objects in tester class (TestDuck->main()), with 2 references pointing to it (lets say d1,d2). Set the tailSize states to 10 and 20. Invoke d1.swim() & d2.swim(). Compile, run and test your understanding so far. Now make d1.size = d2.size. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1.size = 30. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1=d2. SOP d1.size and d2.size to monitor. What has changed and why? Now make d1=d2=null. Can you access any of the 2 objects now? Now do d1= new Duck(); How many objects are reachable? Do you understand the answers to all the questions? If not, please comment.
Class Design (Duck.java)
public class Duck { int tailSize; /*/setter method public void setTailSize(int size) { //no condition mention in question 0 tailSize = size; } //getter method public int getTailSize() { return tailSize; } */ //swim method public void swim() { System.out.println("I will swim now!"); } }
Test Class(TestDuck.java)
public class TestDuck { // main method public static void main(String[] args) { Duck d1 = new Duck(); Duck d2 = new Duck(); d1.tailSize = 10; d2.tailSize = 20; d1.swim(); d2.swim(); d1.tailSize = d2.tailSize; //sop to monitor after assigning d2 size to d1 size System.out.println("SOP d1 Size is "+(d1.tailSize)); System.out.println("SOP d2 Size is "+(d2.tailSize)); //d1.tailSize changed because we are overwriting it with d2 d1.tailSize = 30; //sop to monitor after changing d1 size System.out.println("SOP d1 Size is "+(d1.tailSize)); System.out.println("SOP d2 Size is "+(d2.tailSize)); // value of d1 size change bcz assigning new value d1 = d2; //sop to monitor System.out.println("SOP d1 Size is "+(d1.tailSize)); System.out.println("SOP d2 Size is "+(d2.tailSize)); //value of d1 changed because now d1 hold same address as d2 d1 = d2 = null; /*/sop to monitor Null pointer Exception System.out.println("SOP d1 Size is "+(d1.tailSize)); System.out.println("SOP d2 Size is "+(d2.tailSize)); */ d1 = new Duck(); //only 1 object is reachable now } }
2) A Pen has inkQty (int), color (string) and can be used to write and refill. A text must be given for it to write. A quantity must be given to refill. If there is ink then the pen will write the text given to it (SOP). Refill works by taking in the int qty to add to the existing inkQty. First as a class designer, on paper apply OOAD and arrive at the class design. Then create the class implementation and create a tester class to create 2 pen objects, give it inkQty and ask it to write. First code this on your own. If you cannot get it correctly, then see Pen.java and TestPen.java. Then fix the code and rerun. Do not see the code first.
Pen.java
public class Pen { int inkQty; String colour; //write method input parameter text public void write(String text) { //if ink if(inkQty > 0) { System.out.println("pen writing '"+text+"' in colour "+colour); inkQty--; } else System.out.println("no ink fill first"); } //refill method input parameter quantity public void refill(int qty) { inkQty += qty; } }
TestPent.java
public class TestPen { public static void main(String[] args) { Pen penObj1 = new Pen(); Pen penObj2 = new Pen(); penObj1.inkQty = 2; penObj1.colour = "red"; penObj1.write("hello testing pen"); //ink 1 penObj1.colour = "green"; penObj1.write("hello testing pen2"); //ink empty penObj1.colour = "black"; penObj1.write("hello testing pen3"); //filling 2 unit ink penObj1.refill(2); penObj1.colour = "black"; penObj1.write("hello testing pen4"); } }
3) 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?
Dog.java
public class Dog { //name String name; //size private int size; //public setter method to set data to private instance veriable public void setSize(int n) { //set size here if(n > 0) size = n; else System.out.println("Invalid Size, give a valid size"); } //public getter method to access set data public int getSize() { //return size return size; } //method bark public void bark() { //if > 10 meows if(size > 10) System.out.println("dog "+name+" meows"); //else if(size > 0 && size < 10) System.out.println("bow wow my name is "+name); /*else System.out.println("Invalid Size, give a valid size");*/ } }
TestDog.java
public class TestDog { //main method public static void main(String[] args) { Dog myObj = new Dog(); myObj.name = "Tommy"; myObj.setSize(9); myObj.bark(); myObj.setSize(15); myObj.bark(); Dog myObj1 = new Dog(); myObj1.name = "Tommy"; myObj1.setSize(-12); //checking inside setter myObj1.bark(); } }
4) There are Accounts. Every Account has a number (string), owner (string) and balance (double). You can withdraw(double amt), debit(doubt amt), and checkBalance(). When you withdraw, the balance should reduce accordingly. When you debit, the balance will increase accordingly. Code an Account class and test it by creating 2 account objects? How do you stop over withdrawal?
Account.java
public class Account { String number; String owner; double balance; // can withdraw public void withdraw(double amt) { if(balance > amt && amt > 0) { balance = balance - amt; System.out.println(amt+" withdraw successful"); } else System.out.println("unable to withdraw give correct amt"); } //can debit amount public void debit(double amt) { if(amt > 0 ) { balance = balance + amt; System.out.println(amt+" debit successful"); } else System.out.println("unable to withdraw give correct amt"); } //can check balance public void checkBalance() { System.out.println(owner+" your balance is "+balance); } }
TestAccount.java
public class TestAccount { // main method public static void main(String[] args) { Account obj1 = new Account(); obj1.owner = "Rahul"; obj1.number = "1245687sbi"; obj1.balance = 1000; obj1.checkBalance(); obj1.withdraw(15.50); obj1.debit(200); obj1.checkBalance(); } }
5) There are TVs. A TV has a name and channel that is being displayed. You can increment/decrement channel. You can change the channel to a given number as well. You can ask the TV to display. When a TV is asked to display, it will print the channel num, the volume.TV has volume (int). You have to switch on the TV first before you can operate the channels or increase or decrease the volume. Design and test TV working.
Tv.java
public class Tv { String name; int channel; int volume; boolean isOn; //can display public void display() { if(isOn) System.out.println("watching channel: "+channel+" at volume: "+volume); } //can on public void start() { isOn = true; System.out.println("tv is on now!"); } //channel can be incri public void channelInc() { channel++; } //decrement channel public void channelDec() { channel--; } //change to given no public void channelChange(int n) { channel = n; } //volume can be incri public void volumeInc() { volume++; } //decrement volume public void volumeDec() { volume--; } }
TestTv.java
public class TestTv { public static void main( String[] args) { Tv obj = new Tv(); obj.name = "Mi"; obj.channel = 100; obj.volume = 40; obj.start(); obj.display(); obj.channelInc(); obj.volumeDec(); obj.display(); obj.channelDec(); obj.volumeInc(); obj.display(); obj.channelChange(55); obj.display(); } }
Learn OOPS Concept in Java and Instance Variables :
Learn Getter & Setter in Java
No comments:
For Query and doubts!