Final exams tasks related to JAVA Programming, Exams of Object Oriented Programming

INcluding complex programs,uml,and uml implemented codes.

Typology: Exams

2020/2021

Uploaded on 07/01/2021

soul-recto
soul-recto 🇵🇰

5

(1)

6 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
[Object Oriented
Programming]
University
of
Engineering&
Technology
Taxila
OOP FINAL Viva Answers
SUBMITTED BY:
SHEIKH ABUZAR AMJAD
REG NO:
19-CP-6
SUBMITTED TO:
DR RIZWAN SB
1
Department of Computer Engineering
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Final exams tasks related to JAVA Programming and more Exams Object Oriented Programming in PDF only on Docsity!

[Object Oriented

Programming]

University of Engineering& Technology

Taxila

OOP FINAL Viva Answers

SUBMITTED BY: SHEIKH ABUZAR AMJAD REG NO: 19-CP- SUBMITTED TO: DR RIZWAN SB

Department of Computer Engineering

Q No 01:- Required is a program that reads in various record files with similar structure {name, roll, admission category, email, blood group, technology and domicile} and combines them into single file by removing duplicate entries (if there is any) and ensuring no data loss. Assuming the file type yourself, build appropriate classes in order to do the job. Present your design in class diagrams and do the coding, test and validate your results before dispatching solution. CODE package javaapplication4; /**

  • @author Abuzar sheikh / // Java program to merge two // files into third file import java.io.; import static java.lang.Character.UnicodeBlock.of; import java.io.*; import java.util.HashSet; public class FileMerge { public static void main(String[] args) throws IOException { // BufferedReader object for file1.txt try ( // PrintWriter object for file3.txt PrintWriter pw = new PrintWriter("file3.txt")) { // BufferedReader object for file1.txt BufferedReader br = new BufferedReader(new FileReader("file1.txt")); String line = br.readLine(); // loop to copy each line of // file1.txt to file3.txt while (line != null) { pw.println(line); line = br.readLine(); } br = new BufferedReader(new FileReader("file2.txt")); line = br.readLine();

Q No 02:- The following is a short snippet of code that simulates rolling a 6-sided dice 100 times. There is an equal chance of rolling any digit from 1 to 6. public static void printDiceRolls(Random randGenerator) { for (int i = 0; i < 100; i++) { System.out.println(randGenerator.nextInt(6) + 1); } } public static void main(String[] args) { Random randGenerator = new Random(); printDiceRolls(randGenerator); } Create your own class, LoadedDice, that is derived from Random. The constructor for LoadedDice needs to only invoke Random’s constructor. Override the public int nextInt(int num) method so that with a 50% chance, your new method always returns the largest number possible (i.e., num – 1), and with a 50% chance, it returns what Random’s nextInt method would return. Test your class by replacing the main method with the following: LoadedDice myDice = new LoadedDice(); printDiceRolls(myDice); do you need to change the printDiceRolls method while takes a parameter of type Random. Does polymorphism tell Java to invoke LoadedDice’snextInt() method instead of Random’s nextInt() method.

Answer:

This task can be implemented in two ways i.e. one method is to showthe chance ratio of number

on the dice and 2nd^ way is to show numbers that come up 100 times when dice rolled.

Here is the code for first method:

CODE- / package javaapplication2; /*

  • @author Abuzar sheikh */ import java.util.Random; class LoadedDice extends Random { public LoadedDice() { super(); } } public class RandomClass1 { public static void printDiceRolls(Random randGenerator) { int counts[]=new int[6]; for(int i=0;i<10000;i++) { int output=randGenerator.nextInt(6)+1; counts[output-1]++; } for(int i=0;i<6;i++) { System.out.println( "percentage of "+(i+1)+" is :"+(double)(counts[i]/100));
  • and open the template in the editor. / package Employe; /*
  • @author Abuzar sheikh */ import java.util.Random; class LoadedDice extends Random { public LoadedDice() { super(); } @Override public int nextInt(int number){ int num = super.nextInt(2); //if 0occurs then return 1 if(num==0){ return 1; } else { return super.nextInt(number); } } public static void printDiceRolls(Random randGenerator) { for (int i = 0; i < 100; i++) { System.out.println(randGenerator.nextInt(6) + 1); } } public static void main(String[] args) { Random randGenerator = new Random(); System.out.println("randGenerator Object"); printDiceRolls(randGenerator); LoadedDice myDice= new LoadedDice(); System.out.println("myDice Object"); printDiceRolls(myDice); } }

OUTPUT Q No 03:- Implement the animal class hierarchy given here

public boolean GetGender() { return(gender); } public boolean IsMale() { return(gender==false); } public boolean IsFemale() { return(gender==true); } public boolean IsMammal() { return(mammal==true); } } class Duck extends Animal { protected String beakColor; public Duck() { beakColor="yellow"; } public void SetBeakColor(String colorStr) { beakColor = new String(colorStr); } public void swim() { System.out.println("duck swimming... "); } public void Quack() { System.out.println("Quack"); } } class Fish extends Animal { protected int size; protected boolean canEat; public void SetSize(int n) { size=n; } public void SetCanEat() { canEat=true; } public void SetCannotEat() { canEat=false; } public int GetSize() { return(size); } public boolean CanEat() { return(canEat==true); } public void swim() { System.out.println(" fish is swimming ");

class Zebra extends Animal { protected boolean is_wild; public void SetWild() { is_wild=true; } public void SetTame() { is_wild=false; } public boolean IsWild() { return(is_wild==true); } public void run() { System.out.println(" zebra is running..."); } } class MyZoo { public static void main(String args[]) { Duck duck = new Duck(); Zebra zebra = new Zebra(); Fish fish = new Fish(); duck.swim(); zebra.run(); fish.swim(); duck.SetAge(1); fish.SetAge(2); zebra.SetAge(4); System.out.println(" duck age = " + duck.GetAge() + " : zebra age = " + zebra.GetAge()); } } OUTPUT