0% found this document useful (0 votes)
16 views7 pages

Oops University Asked Programs

OOPS LAB MANUAL

Uploaded by

nathanarokia9
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)
16 views7 pages

Oops University Asked Programs

OOPS LAB MANUAL

Uploaded by

nathanarokia9
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/ 7

1.

Write a java program to find the greatest of three number(6) (Dec 2022)

import java.util.Scanner;

public class LargestNumberExample1

public static void main(String[] args)

int a, b, c, largest, temp;

//object of the Scanner class

Scanner sc = new Scanner(System.in);

//reading input from the user

System.out.println("Enter the first number:");

a = sc.nextInt();

System.out.println("Enter the second number:");

b = sc.nextInt();

System.out.println("Enter the third number:");

c = sc.nextInt();

//comparing a and b and storing the largest number in a temp variable

temp=a>b?a:b;

//comparing the temp variable with c and storing the result in the variable

largest=c>temp?c:temp;

//prints the largest number

System.out.println("The largest number is: "+largest);

} }

Output:

Enter the first number:

23

Enter the second number:

11

Enter the third number:

67

Largest Number is: 67


2. Write a java program to display the grade of the students by using get() method to get marks and
compute() method to compute the average and display() method to display the grade of the studen2.

public class Student {


private String name;
private int score;
private char grade;
public Student(String name, int score) {
this.name = name;
this.score = score;
this.grade = calculateGrade(score);
}

private char calculateGrade(int score) {


if (score >= 90) return 'A';
else if (score >= 80) return 'B';
else if (score >= 70) return 'C';
else if (score >= 60) return 'D';
else return 'F';
}

// Getters and Setters


public String getName() { return name; }
public int getScore() { return score; }
public char getGrade() { return grade; }
}
3.Write a java programs for library interface with drawbook() returnbook() and checkstatus()
methods.(8)
Program:
package librarybook;
public interface library {
public void drawbook();
public void returnbook();
public void checkstatus();
public void reservebook();
}
package librarybook;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.lang.Exception;

class book implements library


{
Scanner in=new Scanner(System.in);
int BookId[]=new int[2];
String BookName[]=new String[2];
String Author[]=new String[2];
String drawdate[]=new String[2];
String returndate[]=new String[2];
int status[]=new int[2];
public book()
{
for(int i=0;i<BookId.length;i++)
{
System.out.println("Enter the following details for book "+(i+1));
System.out.println("Enter the bookid");
BookId[i]=in.nextInt();
System.out.println("Enter the BookName");
BookName[i]=in.next();
System.out.println("Enter the Author");
Author[i]=in.next();

}}

public void drawbook()


{
String title;
System.out.println("Enter the title to search");
title=in.next();
int flag=0;
for(int i=0;i<BookId.length;i++)
{
if(BookName[i].equals (title)==true & status[i]==0)
{
status[i]=1;
System.out.println("book is found");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date currdate = new Date();
drawdate[i]=dateFormat.format(currdate);

Calendar c = Calendar.getInstance();
c.setTime(currdate);
c.add(Calendar.MONTH, 1);
Date lastdate= c.getTime();
returndate[i]=dateFormat.format(lastdate);
flag=1;
}}

if(flag==0)
System.out.println("book is not found");
}

public void checkstatus()


{
System.out.println("Enter the title to check status");
String title1=in.next();
int flag=0; //book not in library list
for(int i=0;i<BookId.length;i++)
{
if (BookName[i].equals (title1)==true)
{
flag=1; //book present in library list
if(status[i]==1)
{
System.out.println("currently not available");
System.out.println( "The book draw date"+drawdate[i]);
System.out.println( "The book should return on or before:"+returndate[i]);
flag=2; //book not currently present
}}}
if(flag==0)
System.out.println("This book is not found in the library");

if(flag==1)
System.out.println("currently available for drawbook");
}

public void reservebook()


{
System.out.println("Enter the title to reserve book");
String title2=in.next();
int flag=0;
for(int i=0;i<BookId.length;i++)
{
if(BookName[i].equals (title2)==true &status[i]==0)

{
System.out.println("currently available");
status[i]=1;
System.out.println("book is reserved");
flag=1;
}}
if(flag==0)
{
System.out.println("currently not available");
}

public void returnbook()


{
System.out.println("Enter the title of book to be return");
String title3=in.next();
for(int i=0;i<BookId.length;i++)
{
if(BookName[i].equals (title3)==true & status[i]==1)
{
fine(i);
status[i]=0;
System.out.println("the book is returned");
drawdate[i]=returndate[i]=null;
}
}
}

public void fine(int j)


{

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");


Date one;
Date two;
Date currdate = new Date();
String d=dateFormat.format(currdate);
try
{
one =dateFormat.parse(d);
two=dateFormat.parse(returndate[j]);

if(one.getTime()>two.getTime())
{
long difference = (one.getTime()-two.getTime())/86400000;
System.out.println("fine :" +((Math.abs(difference))*5));
}
else
System.out.println("fine : nil");

}
catch(Exception e)
{}

}
}

public class Librarybook {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

book b=new book();

int j,i=1;
Scanner in=new Scanner(System.in);
while( i==1)
{
System.out.println("1.drawbook\n2.checkstatus\n3.returnbook\n4.reservebook");
System.out.println("enter option");
int op=in.nextInt();
switch(op)
{
case 1:
b.drawbook();
break;
case 2:
b.checkstatus();
break;
case 3:
b.returnbook();
break;
case 4:
b.reservebook();
break;
default:
System.out.println("invalid option ");
}
System.out.println("do u want to continue 1.y/2.n ");
j=in.nextInt();
if(j==2)
i=0;
}

output:

Enter the following details for book 1


Enter the bookid
121
Enter the BookName
oops
Enter the Author
aaa
Enter the following details for book 2
Enter the bookid
235
Enter the BookName
java
Enter the Author
sss

1.drawbook
2.checkstatus
3.returnbook
4.reservebook
enter option
1
Enter the title to search
java
book is found
do u want to continue 1.y/2.n
1

1.drawbook
2.checkstatus
3.returnbook
4.reservebook
enter option
2
Enter the title to check status
java
currently not available
The book draw date03/09/2018
The book should return on or before:03/10/2018
do u want to continue 1.y/2.n
1

1.drawbook
2.checkstatus
3.returnbook
4.reservebook
enter option
4
Enter the title to reserve book
oops
currently available
book is reserved
do u want to continue 1.y/2.n
1

1.drawbook
2.checkstatus
3.returnbook
4.reservebook
enter option
3
Enter the title of book to be return
java
fine : nil
the book is returned
do u want to continue 1.y/2.n
2

4. Explain how inner classes and anonymous classes works in java program.

You might also like