file
file
Project File
Output:
Output:
Q4. WAP to print the grace of a student as per given marks
‘A’ for marks>80
‘B’ marks between 70-80
‘C’ marks between 60-70
‘D’ marks between 50-60
‘F’ for marks less than 50.
Output:
Q5. WAP to find the roots of a quadratic equation.
Output:
Q6. Print (a+(b*c))/(b-c) taking a, b, c as input.
Output:
class Ten
{
public static void main(String []args)
{
System.out.println("Calculator");
int a, b, c;
Scanner scanner = new Scanner(System.in);
System.out.println("First value: ");
a = scanner.nextInt();
System.out.println("Second value: ");
b = scanner.nextInt();
System.out.println("Function to be performed (+, -, *, /)");
char op = scanner.next().charAt(0);
if (op == '+')
{
c = a + b;
System.out.print(c);
}
else if (op == '-')
{
c = a - b;
System.out.print(c);
}
else if (op == '*')
{
c = a * b;
System.out.print(c);
}
else if (op == '/')
{
c = a * b;
System.out.print(c);
}
else
{
System.out.println("Can't perform the specified action.");
}
}
}
Output:
Q11. Create calculator using Switch.
import java.util.Scanner;
case 2:
result = a - b;
System.out.println("The difference is: " + result);
break;
case 3:
result = a * b;
System.out.println("The product is: " + result);
break;
case 4:
if (b != 0) {
result = a / b;
System.out.println("The division is: " + result);
} else {
System.out.println("Error");
}
break;
default:
Output:
Output:
Q13. WAP to print whether a given number is positive, negative or
zero.
Output:
Q16. WAP to print the name of the month and the number of days for
given no (1-12). E.g.- 3- March 31 days.
import java.util.Scanner;
class Sixteen
{
public static void CheckYear(){
int Year;
System.out.print("Write the year: ");
Scanner scanner = new Scanner(System.in);
Year = scanner.nextInt();
if (Year%4==0){
if (Year%400==0){
System.out.print("February : 29 days");
}
else if (Year%100==0){
System.out.print("February : 28 days");
}
else {
System.out.print("February : 29 days");
}
}
else {
System.out.print("February : 28 days");
}
scanner.close();
}
public static void main(String []args)
{
int Number;
System.out.print("Write a Number between 1 and 12: ");
Scanner scanner = new Scanner(System.in);
Number = scanner.nextInt();
if (Number == 1){
System.out.print("January : 31 days");
}
else if (Number == 2){
CheckYear();
}
else if (Number == 3){
System.out.print("March : 31 days");
}
else if (Number == 4){
Output:
Q17. Create a calendar, enter date to get its previous date, next date,
and day.
import java.util.Scanner;
int daysInMonth = 0;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
daysInMonth = 31;
break;
case 4: case 6: case 9: case 11:
daysInMonth = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
}
// Print
System.out.println("First matrix:");
printMatrix(matrix1);
System.out.println("Second matrix:");
printMatrix(matrix2);
// Addition
if (rows1 == rows2 && columns1 == columns2) {
int[][] sumMatrix = new int[rows1][columns1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns1; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("Sum of matrices:");
printMatrix(sumMatrix);
} else {
System.out.println("Matrices cannot be added.");
}
// Multiplication
if (columns1 == rows2) {
int[][] productMatrix = new int[rows1][columns2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
productMatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Product of matrices:");
printMatrix(productMatrix);
} else {
System.out.println("Matrices cannot be multiplied.");
}
}
double calculateArea() {
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
void printArea() {
double area = calculateArea();
System.out.println("The area of the triangle is: " + area);
}
void getType(){
if(side1==side2 && side2==side3){
System.out.println("Equilateral");
}
else{
if(side1!=side2 && side2!=side3 && side3!=side1){
System.out.println("Scalene");
}
else{
System.out.println("Isosceles");
}
}
}
public static void main(String[] args) {
// Example usage
Nineteen triangle = new Nineteen("Red", 5.0, 7.0, 8.0);
triangle.printArea();
triangle.getType();
}
}
Output:
int choice;
do {
System.out.println("----- Account Menu ");
System.out.println("1. Create Account");
System.out.println("2. Check Balance");
System.out.println("3. Update Balance");
System.out.println("4. Withdraw");
System.out.println("5. Deposit");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Account Number: ");
int accountNumber = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Initial Balance: ");
Float balance = scanner.nextFloat();
System.out.print("Enter Interest Rate: ");
Float interestRate = scanner.nextFloat();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter Account Type (Savings/Current): ");
String accountType = scanner.nextLine();
account = new Twenty(accountNumber, name, balance,
interestRate, accountType);
System.out.println("Account Created Successfully!");
break;
case 2:
if (account != null) {
account.checkBalance();
} else {
System.out.println("No account found");
}
break;
case 3:
if (account != null) {
System.out.print("Enter the
amount to update the
");
balance:
Float amount =
scanner.nextFloat();
account.updateBalance(amoun
t);
} else {
System.out.println("No
account found");
}
break;
case 4:
if (account != null) {
System.out.print("Enter the amount to withdraw: ");
Float amount = scanner.nextFloat();
account.withdraw(amount);
} else {
System.out.println("No account found");
}
break;
case 5:
if (account != null) {
System.out.print("Enter the amount to deposit: ");
Float amount = scanner.nextFloat();
account.deposit(amount);
} else {
System.out.println("No account found");
}
break;
case 6:
System.out.println("Exiting");
break;
default:
System.out.println("Invalid choice");
}
System.out.println();
Distance(double d1) {
this.d1 = d1;
}
double getD1() {
return d1;
}
void print() {
System.out.println("Distance: " + d1 + " units");
}
}
void print() {
System.out.println("Distance: " + getD1() + " inches");
}
@Override
public void display() {
super.display();
System.out.println("Salary: " + salary);
System.out.println("Designation: " + designation);
}
}
float calculatePay() {
return workHours * ratePerHour;
}
@Override
public void display() {
super.display();
Float pay = calculatePay();
System.out.println("Pay: " + pay);
}
}
System.out.println();
class MotorVehicle{
class TwentyFour{
public static void main(String[] args) {
Output:
Q25. An educational institution wishes to maintain a database of its
employees. The database is divided into several classes whose
hierarchical relationships are shown in the figure below. The figure
also shows the minimum information required for each class. Specify
all the classes and define methods to create the database and retrieve
individual information as and when required. Implement this
application by creating multiple classes and storing them in different
files. Also, write a driver class called college and illustrate the
execution of this application.
class Staff{
String code, name;
Staff(String code, String name){
this.code = code;
this.name = name;
}
void print(){
System.out.println("Code is : " + code);
System.out.println("Name is : " + name);
}
}
}
}
Output:
Q26. Create a file to read input from file 1 and copy its content to file
2.
import java.io.*;
public class copycontent {
public static void main(String[] args) throws IOException {
File F1 = new File ("F1.txt");
File F2 = new File ("F2.txt");
FileInputStream fis = new FileInputStream(F1);
FileInputStream fis1 = new FileInputStream(F2);
int num = fis.available();
int num1 = fis1.available();
byte b1[] = new byte[num];
fis.read(b1);
byte b2[] = new byte[num1];
fis1.read(b2);
String s = new String(b1);
String s1 = new String(b2);
System.out.println("File 1 Contents");
System.out.println(s);
System.out.println("File 2 Contents");
System.out.println(s1);
s1 = s;
fis.close();
fis1.close();
Output:
_________________________________________________________________
Q27. WAP to count the no. of blank spaces from file and delete them
import java.io.*;
public class blank {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents");
System.out.println(s);
byte b1[] = new byte[num];
String bs = " ";
int count = 0;
for (int i = 0; i < num; i++) {
int j = 0;
if (s.charAt(i) == ' ') {
count++;
} else {
b[j] = (byte) s.charAt(i);
j++;
bs = bs + s.charAt(i);
}
}
Output:
Q28. WAP to read a file and count the number of vowels.
import java.io.*;
public class vowelcount {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents");
System.out.println(s);
byte b1[] = new byte[num];
int count = 0;
for (int i=0; i < num; i++){
if(s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O'
||s.charAt(i) == 'U' || s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i)
== 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u')
{
countv++;
}
}
Output:
Q29. WAP to read two files and concatenate the input into the third
file.
import java.io.*;
public class concatenate {
public static void main(String[] args) throws IOException {
File F1 = new File ("F1.txt");
File F2 = new File ("F2.txt");
File F3 = new File ("F3.txt");
FileInputStream fis = new FileInputStream(F1);
FileInputStream fis1 = new FileInputStream(F2);
FileInputStream fis2 = new FileInputStream(F3);
int num = fis.available();
int num1 = fis1.available();
int num2 = fis2.available();
byte b1[] = new byte[num];
fis.read(b1);
byte b2[] = new byte[num1];
fis1.read(b2);
byte b3[] = new byte[num2];
fis2.read(b3);
String s = new String(b1);
String s1 = new String(b2);
String s2 = new String(b3);
s2= s+s1;
System.out.println("File 1 Contents");
System.out.println(s);
System.out.println("File 2 Contents");
System.out.println(s1);
System.out.println("File 3 Contents (concatenated file1 and file2)");
System.out.println(s2);
fis.close();
fis1.close();
fis2.close();
fos.close();
fos1.close();
fos2.close();
}
}
Output:
Q30. WAP to read a file and replace a vowel with blank space.
import java.io.*;
public class intro {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents: ");
System.out.println(s);
byte b1[] = new byte[num];
int countv = 0;
String bs = " ";
for (int i=0; i < num; i++){
if(s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O'
||s.charAt(i) == 'U' || s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i)
== 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u')
{
countv++;
int count = 0;
for (int j = 0; j < num; j++) {
int k = 0;
if (s.charAt(j) == ' ') {
count++;
} else {
b[k] = (byte) s.charAt(j);
k++;
bs = bs + s.charAt(j);
}
}
}
}
Output:
_________________________________________________________________
Q31. WAP to create a file to save employee details, designagon,
salary, department. Write the details in the file as an object and read
and print.
import java.io.*;
class Employee_Details implements Serializable{
String Name;
String Designation;
int Salary;
String Department;
Employee_Details (String n, String des, int s, String dep){
this.Name = n;
this.Designation = des;
this.Salary = s;
this.Department = dep;
}
void Read_Details (String n, String des, int s, String dep){
Name = n;
Designation = des;
Salary = s;
Department = dep;
}
void Print_Details(){
System.out.println("Name of the Employee" + Name);
System.out.println("Designation of the Employee" + Designation);
System.out.println("Salary of the Employee" + Salary);
System.out.println("Department of the Employee" + Department);
}
}
public class main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Employee_Details e1 = new Employee_Details("abc", "Manager", 10000, "Sales");
Employee_Details e2 = new Employee_Details("def", "Manager", 100000, "Marketing");
FileOutputStream fos = new FileOutputStream ("Employee_Details");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.close();
Q32. WAP that reads two integers and runs a loop for calculating ab .
You must check if b=0, then throw an exception and handle it by a/b.
Catch the exception and let the code continue until there is no
exception. Give ‘Hi’ and ‘Bye’ statements for every block of code.
import java.util.*;
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero!");
}
int result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
}
System.out.println("a^b = " + result);
System.out.println("Bye try!");
System.out.println(" ");
break;
}
catch (ArithmeticException a) {
System.out.println("Hi catch!");
System.out.println(" ");
System.out.println("Exception: " + a.getMessage());
System.out.println("Handled by performing a/b.");
System.out.println("Answer: Infinity");
System.out.println("Bye catch!");
System.out.println(" ");
}
}
System.out.println("Bye Main!");
}
}
Output: