0% found this document useful (0 votes)
22 views

Object Oriented Programming

The document provides sample code for object-oriented programming questions on inheritance, method overriding, and iterators in Java. It includes 3 easy level questions on inheritance and 2 medium level questions - one on method overriding and another using iterators. The questions are meant as assignments for a pre-placement training on Java.

Uploaded by

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

Object Oriented Programming

The document provides sample code for object-oriented programming questions on inheritance, method overriding, and iterators in Java. It includes 3 easy level questions on inheritance and 2 medium level questions - one on method overriding and another using iterators. The questions are meant as assignments for a pre-placement training on Java.

Uploaded by

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

Pre-Placement Training - Java

Assignment 5 - Object Oriented Programming

● Easy Level questions (Attempt any 2) -


○ Inheritance 1 [ YT Link ]
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Animal{
void walk()
{
System.out.println("I am walking");
}
}

class Bird extends Animal


{
void fly()
{
System.out.println("I am flying");
}
void sing() {
System.out.println("I am singing");
}
}

public class Solution{

public static void main(String args[]){

Bird bird = new Bird();


bird.walk();
bird.fly();
bird.sing();

}
}

○ Inheritance 2
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

//Write your code here


class Arithmetic {
public int add(int a, int b){
return a+b;
}
}
class Adder extends Arithmetic{

public class Solution{


public static void main(String []args){
// Create a new Adder object
Adder a = new Adder();

// Print the name of the superclass on a new line


System.out.println("My superclass is: " +
a.getClass().getSuperclass().getName());

// Print the result of 3 calls to Adder's


`add(int,int)` method as 3 space-separated integers:
System.out.print(a.add(10,32) + " " + a.add(10,3)
+ " " + a.add(10,10) + "\n");
}
}

● Medium Level Questions (Attempt any 2) -


○ Method Overriding 2
import java.util.*;
import java.io.*;

class BiCycle{
String define_me(){
return "a vehicle with pedals.";
}
}

class MotorCycle extends BiCycle{


String define_me(){
return "a cycle with an engine.";
}

MotorCycle(){
System.out.println("Hello I am a motorcycle, I am "+
define_me());
BiCycle bi=new BiCycle();
String temp=bi.define_me(); //Fix this line

System.out.println("My ancestor is a cycle who is "+


temp );
}

}
class Solution{
public static void main(String []args){
MotorCycle M=new MotorCycle();
}
}
○ Iterator [ YT Link ]
import java.util.*;
public class Main{

static Iterator func(ArrayList mylist){


Iterator it=mylist.iterator();
while(it.hasNext()){
Object element = it.next();
if(element instanceof Integer )//Hints: use
instanceof operator
continue;

break;
}
return it;

}
@SuppressWarnings({ "unchecked" })
public static void main(String []args){
ArrayList mylist = new ArrayList();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 0;i<n;i++){
mylist.add(sc.nextInt());
}

mylist.add("###");
for(int i=0;i<m;i++){
mylist.add(sc.next());
}

Iterator it=func(mylist);
while(it.hasNext()){
Object element = it.next();
System.out.println((String)element);
}
}
}

You might also like