0% found this document useful (0 votes)
3 views10 pages

lab1

The document contains a series of Java programming assignments that cover various topics including calculating area and perimeter of a rectangle, mileage calculation, menu selection using switch statements, summing product prices, checking loan eligibility, and more. Each assignment includes a question, a Java program implementation, and expected outputs. The assignments aim to enhance understanding of Java programming concepts such as loops, arrays, conditionals, and user input handling.

Uploaded by

santhoshaaron25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

lab1

The document contains a series of Java programming assignments that cover various topics including calculating area and perimeter of a rectangle, mileage calculation, menu selection using switch statements, summing product prices, checking loan eligibility, and more. Each assignment includes a question, a Java program implementation, and expected outputs. The assignments aim to enhance understanding of Java programming concepts such as loops, arrays, conditionals, and user input handling.

Uploaded by

santhoshaaron25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Lab Assignment - 1

1. Question:
Take the length and breadth of a rectangle as input, and print its area and perimeter.(use scanner
class to read the inputs)

Program:
import java.util.Scanner;

public class rectangle {


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

System.out.print("Enter length: ");


int length = scanner.nextInt();

System.out.print("Enter Breadth: ");


int breadth = scanner.nextInt();

System.out.println("Area of rectangle is " + length * breadth);


System.out.println("Perimeter of rectangle is " + 2 * (length + breadth));
}
}

Output:

2. Question:
A car traveled distance kilometers using litres of petrol. Calculate and display the mileage in km/l.
Also, check if the mileage is greater than 15 km/l and print “Fuel E cient” or “Not Fuel E cient”.

Program:

import java.util.Scanner;

public class program2 {


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

System.out.print("Enter distance travelled: ");


int distance = scanner.nextInt();

System.out.print("Enter amount of Petrol: ");


int petrol = scanner.nextInt();

int mileage = distance / petrol;


System.out.println("Mileage is " + mileage);

if(mileage > 15) {


System.out.println("E cient");
}
else {
System.out.println("Ine cient");
}
}
}

Output:

3. Question:
Write a Java program that displays a food menu to the user and prompts them to select an item
by entering a number corresponding to their choice. The menu includes the following items: 1 for
Burger (₹80), 2 for Pizza (₹120), 3 for Sandwich (₹50), and 4 for Co ee (₹40). Use a switch
statement to determine and display the price of the selected item based on the user’s input. If the
input does not match any of the valid menu options (1 to 4), the program should print "Invalid
menu option" to indicate that the selection is not available.

Program:
import java.util.Scanner;

public class program3 {


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

System.out.print("Enter your choice: ");


int choice = scanner.nextInt();
ffi
ffi
ff
switch(choice) {
case 1:
System.out.println("Your ordered item is Burger");
System.out.println("Price: $80");
break;
case 2:
System.out.println("Your ordered item is Pizza");
System.out.println("Price: $120");
break;
case 3:
System.out.println("Your ordered item is Sandwich");
System.out.println("Price: $50");
break;
case 4:
System.out.println("Your ordered item is Co ee");
System.out.println("Price: $40");
break;
default:
System.out.println("Invalid option");
}
}
}

Output:

4. Question:
Allow the user to enter product prices repeatedly until they enter 0 to nish. Print the total amount
at the end. Use a while or do-while loop.

Program:
import java.util.Scanner;

public class program4 {


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

int total = 0;
while(true) {
System.out.print("Enter price: ");
int price = scanner.nextInt();
ff
fi
total += price;

if(price == 0) {
break;
}
}
System.out.println("Your total is " + total);
}
}

Output:

5. Question:
Write a program that stores the weights of packages using an array. Use a for-each loop to count
how many packages weigh more than 10 kg.

Program:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[5];
int count = 0;
for(int i = 0; i < arr.length; i++) {
System.out.println("Enter the weight: ");
int weight = sc.nextInt();
arr[i] = weight;
if(weight > 10) {
count++;
}
}
System.out.println("More than 10kg: "+ count);
}
}

Output:
6. Question:
Write a Java program that checks whether a person is eligible for a car loan based on their age
and annual income, using the ternary operator for decision-making. The program should take the
person's age and income as input. To be eligible, the person must be at least 21 years old and
have an annual income of ₹2,50,000 or more. Use nested ternary expressions to evaluate the
conditions and display one of the following messages based on the result: "Eligible for car loan" if
both conditions are met, "Not eligible: Age too low" if only the age condition fails, "Not eligible:
Income too low" if only the income condition fails, or "Not eligible: Age and income too low" if
both conditions fail.

Program:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter age: ");
int age = sc.nextInt();
System.out.println("Enter salary: ");
int salary = sc.nextInt();
String eligible = (age < 21 && salary < 250000) ? "Not eligible: Age and income too low " :
(age >= 21 && salary < 250000) ? "Not eligible: Income too low" : (age < 21 && salary >=
250000) ? "Not eligible: Age too low" : "Eligible";
System.out.println(eligible);
}
}

Output:
7. Question:
Write a Java program that checks a user's access permissions based on an integer value
representing combined access rights using bitwise operators. In this system, permissions are
assigned as follows: Read = 1, Write = 2, and Execute = 4. A user may have one or more
permissions, represented as the sum of these values (e.g., a value of 5 means the user has Read
and Execute permissions). The program should take this integer input and use the bitwise AND (&)
operator to determine whether each individual permission (Read, Write, Execute) is granted. For
example, if the input is 5, the binary representation is 101, meaning the user has Read and
Execute, but not Write permission. The program should output which permissions are available
based on this check.

Program:
import java.util.Scanner;

class Main {
public static void main(String[] args) {
int r = 1, w = 2, e = 4;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
if((choice & r) != 0) System.out.println("You can Read");
if((choice & w) != 0) System.out.println("You can Write");
if((choice & e) != 0) System.out.println("You can Execute");
}
}

Output:
8. Question:
Write a Java program that reads a decimal (base-10) number from the user and converts it into its
binary (base-2) equivalent using a loop-based approach only. The program should repeatedly
divide the number by 2, storing the remainders, and then construct the binary representation by
reversing the order of these remainders. Finally, it should display the binary form of the given
decimal number without using any built-in conversion methods.

Program:
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
String binary = "";
while(num > 0) {
binary = (num % 2) + binary;
num = num / 2;
}
System.out.println("The binary equivalent is " + binary);
}
}

Output:

9. Question:
Write a Java program that reads integers into an array
and removes duplicates using only basic arrays and
loops. The program should accept input from the user,
check each element for prior occurrence, and store only unique values in a new array. Finally, it
should display the array without duplicates, preserving the order of rst appearances.

Program:
import java.util.Scanner;

class Main {
public static void main(String[] args) {
int [] arr = new int[5];
Scanner sc = new Scanner(System.in);
System.out.println("Enter array elements: ");
for(int i = 0; i < 5; i++) {
arr[i] = sc.nextInt();
}

System.out.println("Unique elements are ");


fi
int [] unique = new int[5];
int uniqueCount = 0;
for(int j = 0; j < 5; j++) {
int current = arr[j];
boolean dup = false;

for(int k = 0; k < uniqueCount; k++) {


if(current == unique[k]) {
dup = true;
break;
}
}

if(!dup) {
unique[uniqueCount] = current;
System.out.print(unique[uniqueCount] + " ");
uniqueCount++;
}
}
}

Output:

10. Question:
Write a Java program to perform matrix multiplication between two matrices provided by the user.
The program should rst prompt the user to enter the dimensions of the rst matrix (number of
rows and columns) and the second matrix (number of columns only, assuming the number of rows
in the second matrix is equal to the number of columns in the rst matrix, which is required for
multiplication). Then, the program should read the elements of both matrices. Using nested loops,
it should multiply the matrices according to the rules of matrix multiplication and store the result in
a new matrix. Finally, the program should display the resulting product matrix in a formatted
manner.

Program:
import java.util.Scanner;

class Main {
public static void main(String[] args) {
fi
fi
fi
Scanner sc = new Scanner(System.in);

System.out.println("Enter the size for matrix 1: ");


int row1 = sc.nextInt();
int col1 = sc.nextInt();
int row2 = col1;

System.out.println("Enter the size for matrix 2 (only for column): ");


int col2 = sc.nextInt();
int [][] mat1 = new int[row1][col1];
int [][] mat2 = new int[row2][col2];

int [][] result = new int[row1][col2];

System.out.println("Enter elements for matrix 1: ");


for(int i = 0; i < row1; i++) {
for(int j = 0; j < col1; j++) {
mat1[i][j] = sc.nextInt();
}
}

System.out.println("Enter elements for matrix 2: ");


for(int k = 0; k < row2; k++) {
for(int l = 0; l < col2; l++) {
mat2[k][l] = sc.nextInt();
}
}

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


for(int j = 0; j < col2; j++) {
result[i][j] = 0;
for(int k = 0; k < col1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

for(int k = 0; k < row1; k++) {


System.out.println("");
for(int l = 0; l < col2; l++) {
System.out.print(" " + result[k][l]);
}
}
}
Output:

You might also like