Java
Java
import java.util.Scanner;
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
{
int n,arm=0,r,c;
Scanner scanner = new Scanner(System.in);
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");
}
}
import java.util.Scanner;
public class PatternDisplay {
public static void main(String[] args) {
int rows = 4; // Number of rows in the pattern
import java.util.Scanner;
StringStatus(String str){
char ch = str.charAt(i);
upper++;
lower++;
number++;
spaces++;
else
special++;
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(float s) {
return s * s;
}
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();
}
}
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);
}
}
interface Vehicle
{
int speed;
int gear;
@Override
gear = newGear;
@Override
@Override
int speed;
int gear;
@Override
public void changeGear(int newGear){
gear = newGear;
@Override
@Override
class PresentState {
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
bicycle.printStates();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
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);
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.");
}
}
import java.util.Scanner;
class Table {
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;
int choice;
do {
System.out.println("4. Exit");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
students.add(nameToAdd);
break;
case 2:
} else {
break;
case 3:
System.out.println("List of Students:");
if (students.isEmpty()) {
} else {
System.out.println(student);
break;
case 4:
System.out.println("Exiting...");
break;
default:
break;
scanner.close();