Lab Assessment 01
Lab Assessment 01
Java Basics
Our university library charges a fine for every book returned late. For first
5 days the fine is Rs 2 per day, for 6-10 days fine is three rupees per day
and above 10 days fine is 5 rupees per day. If you return the book after 30
days, it is 50% cost of the book. Write a Java program to accept the
number of days the member is late to return the book and display the fine
appropriate message.
Code:
import java.util.*;
public class ques1 {
public static void main(String[] args) {
int fine;
Scanner s = new Scanner(System.in);
int days;
System.out.print("Enter the number of days: ");
days = s.nextInt();
System.out.print("Enter the cost of the book: ");
int cost = s.nextInt();
if(days<=5)
{
fine=2*days;
System.out.println("The fine is: "+ fine);
}
else if(days>=6 && days<=10)
{
fine=3*days;
System.out.println("The fine is: "+ fine);
}
else if(days>=10 && days<30)
{
fine=5*days;
System.out.println("The fine is: "+ fine);
}
else
{
fine=cost/2;
System.out.println("The fine is: "+ fine);
}
}
}
Input and Output:
Question 2:
Java Arrays
Write a Java Program to create an array of positive numbers from user. The
program should print odd numbers in even position and even number in
odd position. Also total count of odd and even numbers.
Code:
import java.util.Scanner;
public class ques2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double t;
int mat[] = new int[4];
int tutors[][]=new int[4][20];
for(int i = 0; i < mat.length; i++) {
Question 3:
Java Strings
Read 5 registration numbers of VIT Students as input and count the
number of Students from BCE branch in the input given.
Note: Register Number format for example: 17BCE2100.
First two characters specifies admitted year and next three characters
indicates the branch admitted. BCE – BTech CSE.
Also display the students count belongs to programme category as
mentioned below:
BCE, BCB ,BEE - Programme Starts with first letter 'B' indicates BTech
MIS, MIC, MID -Programme Starts with 'MI' indicates MTech Integrated
Code:
import java.util.regex.*;
import java.util.*;
public class ques3 {
public static void main(String[] args) {
String s="BCE";
int count_bce=0;
int count_b=0;
int count_m=0;
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++) {
System.out.println("Enter the "+i+1+"
registration number.");
String string =sc.nextLine();
if(Pattern.matches("..B......",string)) {
System.out.println("The student is pursuing
BTech.");
count_b=count_b+1;
}
else if(Pattern.matches("..MI.....",string)) {
System.out.println("The student is pursuing
MTech Integrated.");
count_m=count_m+1;
}
if(Pattern.matches("..BCE....",string)) {
count_bce=count_bce+1;
}
}
System.out.println("The number of Students from
Btech is: " +count_b);
System.out.println("The number of Students from
MTech is: " +count_m);
System.out.println("The number of Students from
BCE branch is: " +count_bce);
}
}