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

Assignment 7

Uploaded by

dattatreyasaha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 7

Uploaded by

dattatreyasaha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Assignment 7

Concept of Class, Object, Method Overloading, Recursion & Array of Objects


A. Create a class Room which will store width, height and breadth of the room
in three variables. Create another class Roomdemo which will use earlier class,
create instances of rooms, set the values of variables and would calculate
volume of the rooms.
Program Code:
import java.util.Scanner;
class Room{
int w, h, b, vol;
public void calarea(int x, int y, int z)
{
w = x;
h = y;
b = z;
vol = w*h*b;
System.out.print("The area is " + vol);
}
}
public class Assgn7a {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Room area = new Room();
System.out.print("Enter the value of width: ");
area.w = sc.nextInt();
System.out.print("Enter the value of height: ");
area.h = sc.nextInt();
System.out.print("Enter the value of breadth: ");
area.b = sc.nextInt();
System.out.println("The Width is " + area.w);
System.out.println("The Height is " + area.h);
System.out.println("The Breadth is " + area.b);
area.calarea(area.w, area.h, area.b);
sc.close();
}
}
Compilation:
>javac Assgn7a.java
Output:
>java Assgn7a
Enter the value of width: 5
Enter the value of height: 3
Enter the value of breadth: 8
The Width is 5
The Height is 3
The Breadth is 8
The area is 120
B. Write a java program to solve the Tower of Hanoi problem for n disks (n
should be taken as keyboard input) using recursion. Create a separate class to
define the non-static recursive function TOH(int, char, char, char).
Program Code:
import java.util.Scanner;
class TowerOfHanoi{
public void TOH(int n, char soc, char des, char aux){
if(n == 1){
System.out.println("Move Disk 1 from " + soc + " to " + des + " using " +
aux);
return;
}
TOH(n = 1, soc, aux, des);
System.out.println("Move Disk " + n + " from" + soc + " to " + des + " using
" + aux);
TOH(n = 1, aux, des, soc);
}
}
public class Assgn7b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Disks: ");
int n = sc.nextInt();
TowerOfHanoi toh = new TowerOfHanoi();
System.out.println("The Sequence of Moves involved is: ");
toh.TOH(n, 'A', 'C', 'B');
sc.close();
}
}
Compilation:
>javac Assgn7b.java
Output:
>java Assgn7b
Enter the number of Disks: 3
The Sequence of Moves involved is:
Move Disk 1 from A to B using C
Move Disk 1 fromA to C using B
Move Disk 1 from B to C using A
C. Write a java program to display the first n Non-Fibonacci terms using recursion. Create a
separate class to define the non-static recursive function Fibo(int n).
Program Code:

import java.util.Scanner;
class Fibo{
public boolean isFibo(int n, int a, int b){
if(n == a || n == b){
return true;
}
if(b > n){
return false;
}
return isFibo(n, b, a+b);
}
public void disNonFibo(int n, int curNum, int c){
if(c == n){
return;
}
if(!isFibo(curNum, 0, 1)){
System.out.print(curNum + " ");
c++;
}
disNonFibo(n, curNum+1, c);
}
}
public class Assgn7c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Number of Non-Fibonacci Terms to Display:
");
int n = sc.nextInt();
Fibo fibo = new Fibo();
System.out.println("The First " + n + " Non-Fibonacci Terms are: ");
fibo.disNonFibo(n, 1, 0);
sc.close();
}
}
Compilation:
>javac Assgn7c.java
Output:
>java Assgn7c
Enter The Number of Non-Fibonacci Terms to Display: 5
The First 5 Non-Fibonacci Terms are:
4 6 7 9 10
D. Declare a class student that represents the following hierarchical information- id,
name (First, Middle, Last), Gender, DOB (day, month, year), marks of 3 subjects
considering an 1D array (English, Mathematics, Computer Science). To store the
name and DOB use the concept aggregation. Write a java program to store and
display the database of n students by using array of objects. Also write methods to
search a particular student (based on id or name) from array and display his/her
details.
Program Code:
import java.util.Scanner;
class Name {
String f, m, l;
Name(String First, String Middle, String Last) {
f = First;
m = Middle;
l = Last;
}
}
class DOB {
int d, m, y;
DOB(int x1, int y1, int z1) {
d = x1;
m = y1;
y = z1;
}
}
class Student {
Scanner in = new Scanner(System.in);
int id;
Name n;
DOB b;
int marks[] = new int[3];
Student(int id1, String fn, String mn, String ln, int dd, int mm, int yy) {
id = id1;
n = new Name(fn, mn, ln);
b = new DOB(dd, mm, yy);
System.out.println("Enter marks for English, Mathematics and Computer
Science: ");
for(int i = 0; i < 3; i++) {
marks[i] = in.nextInt();
}
}
void display() {
System.out.println("ID="+id);
System.out.println("Name="+n.f+" "+n.m+" "+n.l);
System.out.println("DOB="+b.d+"/"+b.m+"/"+b.y);
System.out.println("Marks in English="+marks[0]);
System.out.println("Marks in Mathematics="+marks[1]);
System.out.println("Marks in Computer Science="+marks[2]);
System.out.println();
}
}
public class Assgn7d {
public static void main(String args[]) {
int i, n, id1, dd, mm, yy;
String fn, mn, ln;
Scanner in = new Scanner(System.in);
System.out.println("Enter the no. of students: ");
n = in.nextInt();
Student arr[] = new Student[n];
for(i = 0; i < n; i++) {
System.out.println("Enter id: ");
id1 = in.nextInt();
System.out.println("Enter first name: ");
fn = in.next();
System.out.println("Enter middle name: ");
mn = in.next();
System.out.println("Enter last name: ");
ln = in.next();
System.out.println("Enter date of birth: ");
dd = in.nextInt();
System.out.println("Enter month of year: ");
mm = in.nextInt();
System.out.println("Enter year of birth: ");
yy = in.nextInt();
arr[i] = new Student(id1, fn, mn, ln, dd, mm, yy);
}
System.out.println("THE DATABASE\n-----------------------------\n");
for(i = 0; i < n; i++) {
arr[i].display();
}
}
}
Compilation: >javac Assgn7d.java
Output: >java Assgn7d
Enter the no. of students:
2
Enter id:
1
Enter first name:
Soura
Enter middle name:
Narayan
Enter last name:
Roy
Enter date of birth:
12
Enter month of year:
7
Enter year of birth:
2000
Enter marks for English, Mathematics and Computer Science:
78 89 99
Enter id:
2
Enter first name:
Rahul
Enter middle name:
N
Enter last name:
Singh
Enter date of birth:
29
Enter month of year:
10
Enter year of birth:
2005
Enter marks for English, Mathematics and Computer Science:
67
78 89
THE DATABASE
-----------------------------

ID=1
Name=Soura Narayan Roy
DOB=12/7/2000
Marks in English=78
Marks in Mathematics=89
Marks in Computer Science=99

ID=2
Name=Rahul N Singh
DOB=29/10/2005
Marks in English=67
Marks in Mathematics=78
Marks in Computer Science=89
E. Write a java program to overload a function rect()
void rect (int, char)- With one integer argument and one-character argument draw a filled
square of side n using character stored in ch.
void rect(int, int, char) – With two integer argument and one character argument draw a
filled rectangle of length l and width b using characters stored in ch.
Program code:
import java.util.Scanner;
class Shapes {
void rect(int a, char ch) {
for(int j = 0; j < a; j++) {
for(int i = 0; i < a; i++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
void rect(int l, int b, char ch) {
for(int i = 0; i < l; i++) {
for(int j = 0; j < b; j++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
}

public class Assgn7e {


public static void main(String args[]) {
Shapes s = new Shapes();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the side of the square: ");
int a = sc.nextInt();
System.out.print("Enter the character: ");
char ch = sc.next().charAt(0);
s.rect(a,ch);
System.out.print("Enter the length of the rectangle: ");
int l = sc.nextInt();
System.out.print("Enter the breadth of the rectangle: ");
int b = sc.nextInt();
System.out.print("Enter the character: ");
ch = sc.next().charAt(0);
s.rect(l, b, ch);
sc.close();
}
}
Compilation: >javac Assgn7e.java
Output: >java Assgn7e
Enter the side of the square: 3
Enter the character: a
aaa
aaa
aaa
Enter the length of the rectangle: 4
Enter the breadth of the rectangle: 6
Enter the character: $
$$$$$$
$$$$$$
$$$$$$
$$$$$$

You might also like