Java Ass 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Name: Tharunya Bala S

Reg. No.: 21BCE0076


Assessment 1

Question 1:
ABC company Ltd. is interested to computerize the pay calculation of their employee in the
form of Basic Pay, Dearness Allowance (DA) and House Rent Allowance (HRA). DA and
HRA are calculated as certain % of Basic pay(For example, DA is 80% of Basic Pay, and
HRA is 30% of Basic pay). They have the deduction in the salary as PF which is 12% of
Basic pay. Propose a computerized solution for the above said problem.

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Basic pay");
int BP=sc.nextInt();
double DA, HRA, PF, Salary;
DA=BP*0.8;
HRA=BP*0.3;
PF=BP*0.12;
Salary=BP+DA+HRA-PF;
System.out.println("Salary is "+Salary);
}
}
Code Screenshot:

Output:

Question 2:
The average runs scored by a batsman in 14 matches is 39. In the next 12 matches the
batsman scored an average of 31 runs. Find his average score in all the 26 matches.

Code:
public class Main
{
public static void main(String[] args) {
int r1,r2,g1,g2,t1,t2;
r1=39;
r2=31;
g1=14;
g2=12;
t1=39*14;
t2=31*12;
float Total=t1+t2;
float average=Total/(g1+g2);
System.out.println("The average runs scored by a batsman in 26 matches is
"+String.format("%.1f",average));
}
}

Code Screenshot:
Output:

Question 3:
Write a program to compute grade of students using if else ladder. Get the input of the
marks from the user. The grades are assigned as followed

If mark=40 and <= 60 Print E

Mark >60 and <=80 Print B

Mark >80 and <=90 Print A

Mark >90 and <=100 Print S

For any other value print “Invalid Mark Entry”

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Mark");
int Mark=sc.nextInt();
if(Mark>90 & Mark<=100)
{
System.out.println("S");
}
else if(Mark>80 & Mark<=90)
System.out.println("A");
else if(Mark>60 & Mark<=80)
System.out.println("B");
else if(Mark>=40 & Mark<=60)
System.out.println("E");
else
System.out.println("Invalid Mark Entry");
};
}

Code Screenshot:

Output:
Question 4:
Get the five digit number from the user as the input and write a menu driven program to
perform the following:
Option1: calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)
Option2: to reverse the number.
Option3: to obtain the sum of the first and last digit of this number.
Option4: to find the count of odd and even numbers in the given input

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
int sum=0;
int rev=0,rem;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the option");
int o=sc.nextInt();
System.out.println("Enter the Number");
int d=sc.nextInt();
if (o==1){
while (d != 0)
{
sum = sum + d % 10;
d = d/10;
}
System.out.println(""+sum);
}
else if(o==2)
{
while(d>0){
rem = d%10;
rev = (rev*10) + rem;
d = d/10;
}
System.out.println(""+rev);
}
else if(o==3){
int temp=d;
int firstDigit=0;
int lastDigit=0;
lastDigit=d%10;
while(d!=0)
{
rem = d%10;
firstDigit=rem;
d=d/10;
}
System.out.println(""+ (firstDigit+lastDigit));
}
else if(o==4){
int even_count = 0;
int odd_count = 0;
while (d > 0)
{
rem = d % 10;
if (rem % 2 == 0)
even_count++;
else
odd_count++;
d = d / 10;
}
System.out.println ("even is "+even_count);
System.out.println ("odd is "+odd_count);
}
};
}

Code Screenshot:
Output:
Question 5:
Write a program to display the maximum and minimum of the given sequence of N numbers.
(Note: The sequence need not be stored. Maximum value can be estimated as you receive
the values from the user)

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,sum=0;
int arr[]=new int[n];
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int max=arr[0],min=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
System.out.println(""+max);
System.out.println(""+min);
}
}
Code Screenshot:

Output:
Question 6:
Write a Java program to delete the numbers divisible by 5 from an array.

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int array[] = new int[10];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<9; i++)
{
array[i]=sc.nextInt();
}
for(int i=0;i<9;i++){
if(array[i]%5!=0){
int[] array1 = new int[10];
array1[i]=array[i];
System.out.println("\n"+array1[i]);
}
}
}
}
Code Screenshot:

Output:
Question 7:
Write a Java Program to create array of objects for three passengers to book railway ticket.
Create a class with member function getdata() to read the information (Ticket_Id,
Customer_name, train_name, seat_no, date, departure_time (hh,min) and confirmation
status (y/n)). Store the ticket information for three passengers. Another member function
search_info() to print the status of the ticket, providing a ticket number.

Code:
import java.lang.*;
import java.util.*;
import java.io.*;

public class Main {


public static void main(String args[]){
Scanner sc = new Scanner(System.in);
passengerDetails pd= new passengerDetails();

while(true)
{
System.out.println("** MENU **");
System.out.println("1. Get Data\n2. Search Info\n3. exit");
int data= sc.nextInt();
sc.nextLine();

switch(data)
{
case 1: pd.getdata(); break;

case 2: System.out.println("Enter Ticket Number");


String Ticket_id = sc.nextLine();
pd.search_info(Ticket_id);
break;
case 3: System.exit(0);
default : System.out.println("Wrong Input Try again\n");
}
}
}

class passenger{

String Ticket_Id,Customer_name, train_name, departure_time,date;


int seat_no;
boolean confirmationStatus;

passenger(String Ticket_Id,String Customer_name,String train_name,int seat_no,String


date,String departure_time, boolean confirmationStatus)

{
this.Ticket_Id= Ticket_Id;
this.Customer_name= Customer_name;
this.train_name= train_name;
this.departure_time= departure_time;
this.date= date;
this.seat_no= seat_no;
this.confirmationStatus= confirmationStatus;
}

class passengerDetails{
private passenger arr[]= new passenger[3];
private Scanner sc= new Scanner(System.in);
public void getdata()
{
String Ticket_Id,Customer_name, train_name, departure_time,date;
int seat_no;
boolean confirmationStatus;

for(int i=0;i<3;i++)
{
System.out.println("**** Passenger "+(i+1)+" ****");
System.out.println("Enter Ticket Id");
Ticket_Id= sc.nextLine();
System.out.println("Enter coustmer Name");
Customer_name= sc.nextLine();
System.out.println("Enter Train Name");
train_name= sc.nextLine();
System.out.println("Enter departure Time");
departure_time= sc.nextLine();
System.out.println("Enter date");
date= sc.nextLine();
System.out.println("Enter seat number");
seat_no= sc.nextInt();
sc.nextLine();
System.out.println("Enter confirmation Status (Y/N)");

if(sc.nextLine().equals("Y"))
confirmationStatus= true;
else
confirmationStatus= false;
arr[i] = new
passenger(Ticket_Id,Customer_name,train_name,seat_no,date,departure_time,confirmation
Status);
}
}
public void search_info(String Ticket_Id){
for(int i=0;i<3;i++)
{
if(arr[i].Ticket_Id.equals( Ticket_Id) )
{
System.out.println("Passenger Found ");
System.out.println("Passenger Name: "+ arr[i].Customer_name);
if(arr[i].confirmationStatus==true)
System.out.println("Ticket is confirmed");
else
System.out.println("Ticket is not confirmed");
return ;
}
}
System.out.println("No Passenger Found");
}
}

Code Screenshot:
Output:

You might also like