0% found this document useful (0 votes)
17 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1)Write a program to input your roll no, name, age, fees paid, sex from keyboard and

print them.(Use of Scanner class and BufferedReader class)

import java.util.Scanner;

public class StudentInfo {

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter your roll number: ");


int rollNo = scanner.nextInt();

scanner.nextLine(); // To consume the newline left by nextInt()

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


String name = scanner.nextLine();

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


int age = scanner.nextInt();

System.out.print("Enter fees paid: ");


double feesPaid = scanner.nextDouble();

scanner.nextLine(); // To consume the newline left by nextDouble()

System.out.print("Enter your sex (M/F): ");


char sex = scanner.nextLine().charAt(0);

System.out.println("\nStudent Details:");
System.out.println("Roll Number: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Fees Paid: " + feesPaid);
System.out.println("Sex: " + sex);

scanner.close();
}
}
2)Write a program to check and print the first Armstrong number starting from your roll
no. Take input from the command line arguments.

import java.util.Scanner;
public class armstrong
{

public static void main(String[] args)


{

int n,arm=0,r,c;
Scanner scanner = new Scanner(System.in);

System.out.println("Enter 3 Digit Number");


n= scanner.nextInt();
scanner.close();
c=n;

while(n>0)
{
r=n%10;
arm=(r*r*r)+arm;
n=n/10;
}

if(c == arm)
System.out.println(" Armstrong number");
else
System.out.println(" not Armstrong number");
}
}

3)Write a program to display following patterns


A
AB1
ABC22
ABCD333

import java.util.Scanner;
public class PatternDisplay {
public static void main(String[] args) {
int rows = 4; // Number of rows in the pattern

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


// Print leading spaces
for (int j = rows - 1; j > i; j--) {
System.out.print(" "); // Two spaces for alignment
}
for (char ch = 'A'; ch <= 'A' + i; ch++) {
System.out.print(ch + " ");
}

for (int j = 1; j <= i; j++) {


System.out.print(i + " "); // Print the row number (1-based)
}
System.out.println();
}
}
}
4)Write a program to create class that stores a string and all its status details such as
number of upper case characters, vowels, and so on

import java.util.Scanner;

public class StringStatus {

static int upper = 0, lower = 0, number = 0, spaces = 0, special = 0;

StringStatus(String str){

for (int i = 0; i < str.length(); i++)

char ch = str.charAt(i);

if (ch >= 'A' && ch <= 'Z')

upper++;

else if (ch >= 'a' && ch <= 'z')

lower++;

else if (ch >= '0' && ch <= '9')

number++;

else if (ch == ' ')

spaces++;

else

special++;

System.out.println("Lower case letters : " + lower);

System.out.println("Upper case letters : " + upper);

System.out.println("Number : " + number);

System.out.println("Spaces : " + spaces);

System.out.println("Special characters : " + special);


}
public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

String str = sc.nextLine();

new StringStatus(str);

}
}
5)Write a program to calculate the area of different shapes (circle, triangle, rectangle,
square) by using method overloading

import java.util.*;

class Area {
double area(int r) {
return 3.14 * r * r;
}

double area(int b, float h) {


return (b * h) / 2;
}

double area(float s) {
return s * s;
}

double area(int w, int l) {


return w * l;
}
}

public class AreaCalculator {


public static void main(String[] args) {
int r, b, w, l;
float h, s;
Scanner sc = new Scanner(System.in);

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


r = sc.nextInt();

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


b = sc.nextInt();

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


h = sc.nextFloat();

System.out.print("Enter side of square: ");


s = sc.nextFloat();

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


l = sc.nextInt();

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


w = sc.nextInt();

Area ac = new Area();

System.out.println("Area of circle: " + ac.area(r));


System.out.println("Area of triangle: " + ac.area(b, h));
System.out.println("Area of square: " + ac.area(s));
System.out.println("Area of rectangle: " + ac.area(w, l));

sc.close(); }
}
7)Write a program to perform matrix multiplication and transpose of a matrix.

import java.util.Scanner;
public class matrix {
public static void main(String[] args) {
int[][] mat = new int[2][2];
int[][] trans = new int[2][2];
Scanner sc = new Scanner(System.in);
System.out.print("Enter matrix elements (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
mat[i][j] = sc.nextInt();
}
}

// Display the original matrix


System.out.println("Matrix elements:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}

// Calculate the transpose


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
trans[j][i] = mat[i][j]; }
}

System.out.println("Transpose of the matrix:");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(trans[i][j] + " ");
}
System.out.println();
}

sc.close(); }
}
10)Create class Student (Roll No, name), class Test(Marks1,Marks2) inherits Student class.
Create class Result which extends Test and has a method named Calculate which finds total
as (Total=Marks1+Marks2) and method which display all the details.

import java.util.Scanner;
interface Test
{
void totalmarks();
}

class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}

class Result extends Student implements Test


{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void totalmarks()
{
int total=(mark1+mark2);
System.out.println ("Total Marks: "+total);
}
void display()
{
super.display();
}
}

public class MultipleInh


{
public static void main(String[] args)
{
Result R = new Result("Shravan",36,85,87);
R.display();
R.totalmarks();
}}
11)Create an Interface ‘Vehicle’ which has abstract methods changeGear(),
speedUp(),applyBrakes().Create a classes like ‘Bike’ ,’Car’ which will implement
functionality of ‘Vehicle’ in their own way
import java.util.Scanner;

interface Vehicle
{

void changeGear(int a);

void speedUp(int a);

void applyBrakes(int a);

class Bicycle implements Vehicle


{

int speed;

int gear;

@Override

public void changeGear(int newGear){

gear = newGear;

@Override

public void speedUp(int increment){

speed = speed + increment;

@Override

public void applyBrakes(int decrement){

speed = speed - decrement;

public void printStates() {

System.out.println("speed: " + speed

+ " gear: " + gear);

class Bike implements Vehicle {

int speed;

int gear;

@Override
public void changeGear(int newGear){

gear = newGear;

@Override

public void speedUp(int increment){

speed = speed + increment;

@Override

public void applyBrakes(int decrement){

speed = speed - decrement;

public void printStates() {

System.out.println("speed: " + speed

+ " gear: " + gear);

class PresentState {

public static void main (String[] args) {

Bicycle bicycle = new Bicycle();

bicycle.changeGear(2);

bicycle.speedUp(3);

bicycle.applyBrakes(1);

System.out.println("Bicycle present state :");

bicycle.printStates();

Bike bike = new Bike();

bike.changeGear(1);

bike.speedUp(4);

bike.applyBrakes(3);

System.out.println("Bike present state :");

bike.printStates();

}
12) Create a abstract class AbstractSum which has abstract methods sumofTwo() and
sumofThree().Create a class Sum which extends AbstractSum and implement its methods.

import java.util.Scanner;
abstract class AbstractSum {
abstract int sumOfTwo(int a, int b);

abstract int sumOfThree(int a, int b, int c);


}

class Sum extends AbstractSum {

int sumOfTwo(int a, int b) {


return a + b;
}

int sumOfThree(int a, int b, int c) {


return a + b + c;
}
}
public class abstractMain {
public static void main(String[] args) {
Sum sum = new Sum();

int result1 = sum.sumOfTwo(5, 10);


System.out.println("Sum of two numbers (5 + 10): " + result1);

int result2 = sum.sumOfThree(5, 10, 15);


System.out.println("Sum of three numbers (5 + 10 + 15): " + result2);
}
}

13)Write a program to create your own exception. The exception will be thrown if number
is odd.
class OddNumberException extends Exception {
public OddNumberException(String message) {
super(message);
}
}
public class CustomExceptionExample {
static void checkEven(int number) throws OddNumberException {
if (number % 2 != 0) {
throw new OddNumberException("The number " + number + " is odd.");
} else {
System.out.println("The number " + number + " is even.");
}
}

public static void main(String[] args) {


int[] numbers = {2, 3, 4, 5, 6};
for (int number : numbers) {
try {
checkEven(number);
} catch (OddNumberException e) {
System.out.println(e.getMessage());
}
}
15)Write a program to print table of any number using Synchronized method.

import java.util.Scanner;
class Table {

synchronized void printTable(int number) {


for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
}

class TableThread extends Thread {


Table table;
int number;

TableThread(Table table, int number) {


this.table = table;
this.number = number;
}

public void run() {


table.printTable(number);
}
}

public class SynchronizedTable {


public static void main(String[] args) {
Table table = new Table();

TableThread thread1 = new TableThread(table, 5);


TableThread thread2 = new TableThread(table, 10);

thread1.start();
thread2.start();
}
}
9)Create a vector Student with their name. Write a program to add, remove and display students name from vector.

import java.util.Scanner;

import java.util.Vector;

public class vector {

public static void main(String[] args) {

Vector<String> students = new Vector<>();

Scanner scanner = new Scanner(System.in);

int choice;

do {

System.out.println("\nStudent Management System");

System.out.println("1. Add Student");

System.out.println("2. Remove Student");

System.out.println("3. Display Students");

System.out.println("4. Exit");

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

choice = scanner.nextInt();

scanner.nextLine();

switch (choice) {

case 1:

System.out.print("Enter student name to add: ");

String nameToAdd = scanner.nextLine();

students.add(nameToAdd);

System.out.println(nameToAdd + " has been added.");

break;

case 2:

System.out.print("Enter student name to remove: ");

String nameToRemove = scanner.nextLine();


if (students.remove(nameToRemove)) {

System.out.println(nameToRemove + " has been removed.");

} else {

System.out.println(nameToRemove + " not found.");

break;

case 3:

System.out.println("List of Students:");

if (students.isEmpty()) {

System.out.println("No students in the list.");

} else {

for (String student : students) {

System.out.println(student);

break;

case 4:

System.out.println("Exiting...");

break;

default:

System.out.println("Invalid choice. Please try again.");

break;

} while (choice != 4);

scanner.close();

You might also like