Abd Java
Abd Java
PRACTICAL 1
1. Write a JAVA Program to make a calculator using switch case. You
can have 5 operations +,-,/,*,%.
Source Code:
import java.util.*;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number : ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number : ");
double num2 = scanner.nextDouble();
System.out.print("Choose an operation (+, -, *, /, %) : ");
char operator = scanner.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
System.out.println("Error! Division by zero is not allowed.");
return;
}
result = num1 / num2;
break;
case '%':
result = num1 % num2;
break;
default:
System.out.println("Error! Invalid operator.");
return;
}
System.out.println("Result: " + result);
}
}
OUTPUT:
Source Code:
import java.util.Scanner;
OUTPUT:
PRACTICAL 2
3. Given a list of marks ranging from 0 to 100. Write a JAVA Program to compute and
print the number of students who have obtained marks
a. in the range of 81-100
b. in the range of 61-80
c. in the range of 41-60
d. in the range of 0-40.
The program should use a minimum number of if statements.
Source Code:
import java.util.*;
public class MarksRange{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,s;
System.out.print("Enter the number of students: ");
s=sc.nextInt();
int[] marks=new int[s];
for(i=0;i<s;i++)
{
System.out.print("Enter marks for student " + (i + 1) + ": ");
marks[i]=sc.nextInt();
}
int[] range=new int[4];
for (int mark : marks)
{
if(mark>=81 && mark<=100)
{
range[0]++;
}
else if(mark>=61 && mark<=80)
{
range[1]++;
}
else if(mark>=41 && mark<=60)
{
range[2]++;
}
else if(mark>=0 && mark<=40)
{
range[3]++;
}
}
System.out.println("Number of students in the range 81-100: " + range[0]);
System.out.println("Number of students in the range 61-80: " + range[1]);
System.out.println("Number of students in the range 41-60: " + range[2]);
System.out.println("Number of students in the range 0-40: " + range[3]);
sc.close();
}
}
OUTPUT:
4. Write a JAVA Program to implement various String class functions such as length(),
concat(), replace(), replaceAll(), trim() etc.
Source Code:
import java.util.*;
OUTPUT:
PRACTICAL 3
Source Code:
import java.util.Scanner;
// Creating matrices
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
// Adding matrices
int[][] sumMatrix = addMatrices(matrix1, matrix2);
scanner.close();
}
return sumMatrix;
}
OUTPUT:
6. Write a Java program to compute the sum of diagonal elements in a m*n matrix.
Source Code:
import java.util.Scanner;
scanner.close();
}
return sum;
}
}
OUTPUT:
Data Members-
*Name of the Depositor
*Type of account
*Account Number
*Balance amount in the account
Methods-
*To assign initial values.
*To deposit an amount
*To withdraw an amount after checking balance.
*To display the name and balance.
Source Code:
import java.util.Scanner;
public class BankAccount
{
private String name,accnt,accntNo;
private double bal;
public BankAccount(String name,String accnt,String accntNo,double bal)
{
this.name=name;
this.accnt=accnt;
this.accntNo=accntNo;
this.bal=bal;
}
public void deposit(double amt)
{
bal+=amt;
System.out.println(amt + " deposited successfully.");
}
public void withdraw(double amt)
{
if(bal>=amt)
{
bal-=amt;
ob.display();
sc.close();
}
}
OUTPUT: