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

java handson

The document contains multiple Java programs covering various topics such as course searching, associate tracking, library book management, student average and grade calculation, array manipulation, and area/volume calculations for different shapes. Each section includes class definitions and methods to perform specific tasks, demonstrating object-oriented programming concepts. The programs utilize user input and handle exceptions to ensure robust functionality.

Uploaded by

ajay579r
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)
7 views

java handson

The document contains multiple Java programs covering various topics such as course searching, associate tracking, library book management, student average and grade calculation, array manipulation, and area/volume calculations for different shapes. Each section includes class definitions and methods to perform specific tasks, demonstrating object-oriented programming concepts. The programs utilize user input and handle exceptions to ensure robust functionality.

Uploaded by

ajay579r
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/ 17

SEARCH A COURSE

import java.util.*;
public class Course
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of course:");
int no_crs=sc.nextInt();
if(no_crs>0)
{
System.out.println("Enter course names:");

String crs[]=new String[no_crs];

for(int i=0;i<no_crs;i++)
{
crs[i]=sc.next();
}

System.out.println("Enter the course to be searched:");


String srch=sc.next();

int flag=0;
for(int i=0;i<no_crs;i++)
{
if(srch.equals(crs[i]))
{
flag++;
}
}

if(flag!=0)
{
System.out.println(srch+" course is available");
}
else
{
System.out.println(srch+" course is not available");
}
}
else
{
System.out.println("Invalid Range");
}

}
}

DREAM TEK COMPANY


i) Associate.java
public class Associate{
private int associateId;
private String associateName;
private String workStatus;

public int getAssociateId()


{
return this.associateId;
}

public void setAssociateId(int associateId)


{
this.associateId=associateId;
}

public String getAssociateName()


{
return this.associateName;
}

public void setAssociateName(String associateName)


{
this.associateName=associateName;
}

public String getWorkStatus()


{
return this.workStatus;
}

public void setWorkStatus(String workStatus)


{
this.workStatus=workStatus;
}

public void trackAssociateStatus(int days)


{
//days=abs(days);

if(days>=1&&days<=20)
{
this.setWorkStatus("Core skills");
}
else if(days>=21 && days<=40)
{
this.setWorkStatus("Advanced modules");
}

else if(days>=41 && days<=60)


{
this.setWorkStatus("Project phase");
}

else
{
this.setWorkStatus("Deployed in project");
}
}
}

ii) Main.java

import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the associate id:");
int ass_Id=sc.nextInt();
sc.nextLine();

System.out.println("Enter the associate name:");


String name=sc.nextLine();

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


int days=sc.nextInt();

Associate obj=new Associate();


obj.setAssociateName(name);
obj.trackAssociateStatus(days);

System.out.println("The associate "+obj.getAssociateName()+" work


status:"+obj.getWorkStatus());

}
}

BOOK MANIPULATION
I) Library.Java
import java.util.ArrayList;
public class Library {
private ArrayList<Book> bookList = new ArrayList<>();

public ArrayList<Book> getBookList() {


return bookList;
}

public void setBookList(ArrayList<Book> bookList) {


this.bookList = bookList;
}

public void addBook(Book book) {


bookList.add(book);
}

public boolean isEmpty() {


return bookList.isEmpty();
}

public ArrayList<Book> viewAllBooks() {


return bookList;
}

public ArrayList<Book> viewBooksByAuthor(String author) {


ArrayList<Book> booksByAuthor = new ArrayList<>();

for (Book book : bookList) {


if (book.getAuthor().contains(author)) {
booksByAuthor.add(book);
}
}
return booksByAuthor;
}

public int countnoofbook(String bname) {


return (int) bookList.stream().filter(book ->
book.getBookName().contains(bname)).count();
}
}
II) Main.java
import java.util.List;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();

while (true) {
System.out.println("1.Add Book\n" +
"2.Display all book details\n" +
"3.Search Book by author \n" +
"4.Count number of books - by book name\n" +
"5.Exit\n" +
"Enter your choice:");
int choice = Integer.parseInt(scanner.nextLine());

switch (choice) {
case 1: {
System.out.println("Enter the isbn no:");
int isbn = Integer.parseInt(scanner.nextLine());

System.out.println("Enter the book name:");


String bookName = scanner.nextLine();

System.out.println("Enter author name:");


String authorName = scanner.nextLine();

Book book = new Book();


book.setIsbnno(isbn);
book.setBookName(bookName);
book.setAuthor(authorName);

library.addBook(book);
break;
}
case 2: {
for (Book book : library.getBookList()) {
System.out.println("ISBN no: " + book.getIsbnno());
System.out.println("Book name: " + book.getBookName());
System.out.println("Author name: " + book.getAuthor());
}

break;
}
case 3: {
System.out.println("Enter the author name:");
String authorName = scanner.nextLine();
List<Book> books = library.viewBooksByAuthor(authorName);

if (books.isEmpty()) {
System.out.println("None of the book published by the author " +
authorName);
} else {
for (Book book : books) {
System.out.println("ISBN no: " + book.getIsbnno());
System.out.println("Book name: " + book.getBookName());
System.out.println("Author name: " + book.getAuthor());
}
}

break;
}
case 4: {
System.out.println("Enter the book name:");
String bookName = scanner.nextLine();
int count = library.countnoofbook(bookName);
System.out.println(count);
}
case 5: {
System.exit(0);
}
}
}
}
}
iii) Book.java
public class Book {
private int isbnno;
private String bookName;
private String author;

public int getIsbnno() {


return isbnno;
}

public void setIsbnno(int isbnno) {


this.isbnno = isbnno;
}

public String getBookName() {


return bookName;
}

public void setBookName(String bookName) {


this.bookName = bookName;
}

public String getAuthor() {


return author;
}

public void setAuthor(String author) {


this.author = author;
}

AVERAGE AND GRADE CALCULATION

i)Student.java
public class Student{
private int id;
private String name;
private float average;
private char grade;
private int marks[];
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public float getAverage(){
return average;
}
public void setAverage(float average){
this.average=average;
}
public char getGrade(){
return grade;
}
public void setGrade(char grade){
this.grade=grade;
}
public int[] getMarks(){
return marks;
}
public void setMarks(int[] marks){
this.marks=marks;
}
public Student(int id,String name,int[] marks){
this.id=id;
this.name=name;
this.marks=marks;
}

public void calculateAvg(){


float n=0.0F;
for(int i=0;i<this.getMarks().length;i++){
n=n+this.marks[i];
}
n=n/this.getMarks().length;
this.setAverage(n);
}
public void findGrade(){
int min=this.marks[0];
for(int i=1;i<this.getMarks().length;i++){
if(this.marks[i]<min){
min=this.marks[i];
}
}
if(min<50){
this.setGrade('F');
}
else if(this.getAverage()<=100 && this.getAverage()>=80){
this.setGrade('O');
}
else{
this.setGrade('A');
}
}

}
ii) Studentmain.java
import java.util.Scanner;
public class StudentMain
{
public static void main(String[] args)
{
Student s=getStudentDetails();
s.calculateAvg();
s.findGrade();
System.out.println("Id:" +s.getId());
System.out.println("Name:" +s.getName());
System.out.println("Average:"+String.format("%.2f",s.getAverage()));
System.out.println("Grade:" +s.getGrade());
}
public static Student getStudentDetails()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the id:");
int id=Integer.parseInt(sc.nextLine());
String name=sc.nextLine();
System.out.println("Enter the no of subjects:");
int n=sc.nextInt();
if(n<=0)
{
while(n<=0)
{
System.out.println("Invalid number of subject");
System.out.println("Enter the no of subjects");
n=sc.nextInt();
}
}
int arr[]=new int[n];
for(int a=0;a<n;a++)
{
System.out.println("Enter mark for subject "+(a+1)+":");
int b=sc.nextInt();
if(b<0||b>100)
{
System.out.println("Invalid Mark");
System.out.println("Enter mark for subject "+(a+1)+":");
b=sc.nextInt();
}
arr[a]=b;
}
Student obj=new Student(id,name,arr);
obj.setId(id);
obj.setName(name);
return obj;
}
}

ARRAY MANIPULATION

import java.util.InputMismatchException;
import java.util.Scanner;

public class ArrayException {


public String getPriceDetails() {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of elements in the array");


int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the price details");

for (int i = 0; i < n; ++i) {


try {
arr[i] = scanner.nextInt();
} catch (InputMismatchException ignore) {
return "Input was not in the correct format";
}
}

System.out.println("Enter the index of the array element you want to access");

try {
int index = scanner.nextInt();
return "The array element is " + arr[index];
} catch (InputMismatchException ignore) {
return "Input was not in the correct format";
} catch (ArrayIndexOutOfBoundsException ignore) {
return "Array index is out of range";
}
}

public static void main(String[] args) {


System.out.println(new ArrayException().getPriceDetails());
}
}

AREA VOLUME CALCULATOR

i) Main.java
import java.util.*;
public class Main
{
public static void main(String args[])
{
Shape[] arr=new Shape[5];
Scanner in= new Scanner(System.in);
String shape;
double l,w,h,r;
for(int i=0;i<5;i++)
{
shape=in.next();
if(shape.equals("Triangle")){
l=in.nextDouble();
w=in.nextDouble();
arr[i]=new Triangle(l,w);
}
else if(shape.equals("Rectangle")){
l=in.nextDouble();
w=in.nextDouble();
arr[i]=new Rectangle(l,w);
}
else if(shape.equals("Sphere")){
r=in.nextDouble();
arr[i]=new Sphere(r);

}
else if(shape.equals("Cube")){

l=in.nextDouble();
h=in.nextDouble();
w=in.nextDouble();
arr[i]=new Cube(l,w,h);
}
}
for(int i=0;i<5;i++)
{
System.out.println("Area" +arr[i].area());
if(arr[i] instanceof Spatial)
System.out.println("Volume" +arr[i].volume());
}
}
}
ii) Cube.java
public class Cube extends Shape implements Spatial
{
private double length;
private double width;
private double height;
public Cube(double a,double b,double c)
{
length=a;
width=b;
height=c;
}
public void setLength(double n)
{
length=n;
}
public double getLength()
{
return length;
}
public void setWidth(double n)
{
width=n;
}
public double getWidth()
{
return width;
}
public void setHeight(double n)
{
height=n;
}
public double getHeight()
{
return height;
}
public double area(){
return 2*((length*width)+(length*height)+(width*height));
}
public double volume(){
return length*width*height;
}
}
iii) Rectangle
public class Rectangle extends Shape
{
private double length;
private double width;
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
public void setLength(double length){
this.length=length;
}
public void setWidth(double width){
this.width=width;
}
public double area(){
return length*width;
}
public double volume(){
return -1;
}
public Rectangle(double l,double w){
setWidth(w);
setLength(l);
}
}

iv) Shape.java
public abstract class Shape
{
public abstract double area();
public abstract double volume();
}
v) Spatial.java
public interface Spatial
{

}
vi) Sphere.java
public class Sphere extends Shape implements Spatial
{
private double radius;
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius=radius;
}
public double area()
{
return 4*Math.PI*radius*radius;
}
public double volume()
{
return 4*Math.PI*radius*radius*radius/3;
}
public Sphere(double r)
{
setRadius(r);
}
}
vii) Triangle.java
public class Triangle extends Shape
{

private double base;


private double height;
public double getBase(){
return base;
}
public double getHeight(){
return height;
}
public void setBase(double base){
this.base=base;
}
public void setHeight(double height){
this.height=height;
}
public double area(){
return 0.5*base*height;
}
public double volume(){
return -1;
}
public Triangle(double b,double h){
setBase(b);
setHeight(h);
}
}

You might also like