Assignment 7
Assignment 7
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();
}
}
}