0% found this document useful (0 votes)
12 views15 pages

Java MK

ouioiu

Uploaded by

lucifergamlng2k1
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)
12 views15 pages

Java MK

ouioiu

Uploaded by

lucifergamlng2k1
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/ 15

Manish Saini

[email protected]

Java Lab Assignments for B.Tech. 4th sem.


CS(CA1,CA2,CB1)

1.WAP to print “Hello World”.

class Hello
{
public static void main(String args[])
{
System.out.println("Hello world!");
}
}

output:
Hello world!

2.WAP that takes five integer numbers from command line arguments and
print their sum.

public class Addition


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

if(args.length!=5)
{
System.out.println("please provide exectly 5 integer as
command line arugment:");

return;
}
int sum=0;

for(String arg:args)
{
sum+=Integer.parseInt(arg);
}

System.out.println("sum="+sum);
}

output:

please provide exectly 5 integer as command line arugment:1 2 3 4 5


sum=15

3.WAP that input three floating point numbers and print maximum among
them.(Use
Scanner class)

import java.util.Scanner;
class Maximum
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);

float a,b,c;

System.out.println("enter a three numbers");

a=s.nextFloat();
b=s.nextFloat();
c=s.nextFloat();

if(a>b && a>c)


{
System.out.println("a is maximum:"+a);
}
else if(b>a && b>c)
{
System.out.println("b is maximum:"+b);
}
else
{
System.out.println("c is maximum:"+c);
}
}
}

output:
enter a three numbers
1.1
1.2
1.3
c is maximum:1.3

4.Write a menu driven program to perform the following operations on an


array of 10 integer
elements1. Display 2. Searching 3. Sorting 4. Reverse 5. Find maximum

import java.util.Scanner;
import javax.swing.*;
class Menudrivenprogram
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int choice,i;
System.out.println("Enter 1 to Display");
System.out.println("Enter 2 to Searching");
System.out.println("Enter 3 to Sorting");
System.out.println("Enter 4 to Reverse");
System.out.println("Enter 5 to maximum");
System.out.println("Enter 6 to exit");
while(true)
{
int arr[]={2,4,7,9,5,6,10,1,8,3};
choice=Integer.parseInt(JOptionPane.showInputDialog("enter
your choice"));
switch(choice)
{
case 1:
System.out.println("Display Array numbers");
for(i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
break;

case 2:
int value,loc=-1;

System.out.println("Enter a value to be search");


value=s.nextInt();
for(i=0;i<arr.length;i++)
{
if(arr[i]==value)
{
loc=i+1;
break;
}
}
if(loc==-1)
{
System.out.println("Element is not Found");
}
else
{
System.out.println("Element found at:"+loc);
}
break;

case 3:
int j;
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

System.out.println("After the
sorting");
for(i=0;i<=9;i++)
{
System.out.println(arr[i]);
}
break;

case 4:
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

System.out.println("After the rivers");


for(i=0;i<=9;i++)
{
System.out.println(arr[i]);
}
break;
case 5:
int max=0;
for(i=0;i<arr.length;i++)
{
if(i==0)
{
max=arr[i];
}
else if(arr[i]>max)
{
max=arr[i];
}
}
System.out.println("The maximum number is
:"+max);
break;
case 6:
System.out.println("exit the program");
s.close();
break;

default:
System.out.println("Invalid choice");
}
}
}

output:
Enter 1 to Display
Enter 2 to Searching
Enter 3 to Sorting
Enter 4 to Reverse
Enter 5 to maximum
Enter 6 to exit
Enter your choice:1
Display Array numbers
2
4
7
9
5
6
10
1
8
3
Enter your choice:2
Enter a value to be search
5
Element found at:5
Enter your choice:3
After the sorting
1
2
3
4
5
6
7
8
9
10
Enter your choice:4
After the rivers
3
8
1
10
6
5
9
7
4
2
Enter your choice:5
The maximum number is :10
Enter your choice:6
exit the program

5.WAP to create a two dimensional Zig-Zag array that store all prime
numbers from 1 to 100
in such way that prime numbers from 1 to 10 in first row, 11 to 20 in
second row and so
on. Print the final array.

import java.util.*;

class ZigzagPrimeArray
{

public static boolean isPrime(int n) {


if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] args)


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

List<Integer> primeNumbers = new ArrayList<>();


for (int num = 1; num <= 100; num++) {
if (isPrime(num)) {
primeNumbers.add(num);
}
}
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
scanner.close();

int cols = 10;


int[][] zigzagArray = new int[rows][cols];

int index = 0;
for (int i = 0; i < rows; i++) {
if (i % 2 == 0) {
for (int j = 0; j < cols; j++) {
if (index < primeNumbers.size() &&
primeNumbers.get(index) <= (i + 1) * 10) {
zigzagArray[i][j] = primeNumbers.get(index++);
} else {
zigzagArray[i][j] = 0; // Fill empty spaces with
0
}
}
} else {
for (int j = cols - 1; j >= 0; j--) {
if (index < primeNumbers.size() &&
primeNumbers.get(index) <= (i + 1) * 10) {
zigzagArray[i][j] = primeNumbers.get(index++);
} else {
zigzagArray[i][j] = 0;
}
}
}
}

for (int[] row : zigzagArray) {


System.out.println(Arrays.toString(row));
}
}
}

output:
Zig-Zag Prime Number Array:
2 3 5 7
11 13 17 19
23 29
31 37
41 43 47
53 59
61 67
71 73 79
83 89
97

6.WAP to create a class “Account” consist of account holder’s name,


account no. and
balance. A parameterized constructor, methods to deposit some amount and
to display the
details. Test your class for two accounts.

class Account
{
private String holderName;
private int accountNumber;
private double balance;

public Assignment6(String hn, int an, double b) {


holderName=hn;
accountNumber=an;
balance=b;
}

public void deposit(double amount) {


balance=amount+balance;
System.out.println(holderName + " deposited: " + amount);
}

public void display() {


System.out.println("Account Holder name: " + holderName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: " + balance);
}
}

class Demo
{
public static void main(String[] args)
{
Account ass1 = new Account("manish", 101, 5000);
Account ass2 = new Account("pawan", 102, 3000);

ass1.deposit(2000);
ass2.deposit(1500);
System.out.println();
ass1.display();
System.out.println();
ass2.display();
}

output:
manish deposited: 2000.0
pawan deposited: 1500.0

Account Holder name: manish


Account Number: 101
Balance: 7000.0

Account Holder name: pawan


Account Number: 102
Balance: 4500.0

7.AP to create a class “Person”, consist of name and age and print the
record of elder one
using this reference.

class Person
{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}

public void display() {


System.out.println("Name: " + name + ", Age: " + age);
}

public int getAge() {


return age;
}
}
public class Maximumage

public static void main(String[] args)


{
Person person1 = new Person("manish", 25);
Person person2 = new Person("Buddy", 30);
person1.display();
person2.display();

System.out.println("Elder Person:");
if (person1.getAge() > person2.getAge())
{
person1.display();
}
else if (person1.getAge() < person2.getAge())
{
person2.display();
}
}
}

output:
Name: manish, Age: 25
Name: Buddy, Age: 30
Elder Person:
Name: Buddy, Age: 30

8.WAP to create a class "Student" consist of name, rollno and percentage


and methods for
I/O. Enter the records of ten students and print them rank wise.(Use
array of objects)

import java.util.Arrays;
import java.util.Scanner;

class Student
{
private String name;
private int rollNo;
private double percentage;

public Student(String name, int rollNo, double percentage) {


this.name = name;
this.rollNo = rollNo;
this.percentage = percentage;
}

public double getPercentage() {


return percentage;
}

public void display(int rank) {


System.out.println("Rank: " + rank + ", Name: " + name + ", Roll
No: " + rollNo + ", Percentage: " + percentage);
}
}
public class Studenttest
{

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[10];

for (int i = 0; i < 10; i++)


{
System.out.println("Enter details for student " + (i + 1) +
":");
System.out.print("Name: ");
String name = scanner.next();
System.out.print("Roll No: ");
int rollNo = scanner.nextInt();
System.out.print("Percentage: ");
double percentage = scanner.nextDouble();
students[i] = new Student(name, rollNo, percentage);
}
scanner.close();

Arrays.sort(students, (s1, s2) ->


Double.compare(s2.getPercentage(), s1.getPercentage()));

System.out.println("\nStudents ranked by percentage:");


for (int i = 0; i < students.length; i++) {
students[i].display(i + 1);
}
}
}

output:
Enter details for student 1:
Name: manish
Roll No: 1
Percentage: 89
Enter details for student 2:
Name: madhu
Roll No: 2
Percentage: 78
Enter details for student 3:
Name: ravi
Roll No: 3
Percentage: 78
Enter details for student 4:
Name: pawan
Roll No: 4
Percentage: 90
Enter details for student 5:
Name: buddy
Roll No: 5
Percentage: 99.99
Enter details for student 6:
Name: madhav
Roll No: 6
Percentage: 67
Enter details for student 7:
Name: amar
Roll No: 7
Percentage: 69
Enter details for student 8:
Name: dabbu
Roll No: 8
Percentage: 79
Enter details for student 9:
Name: ramesh
Roll No: 9
Percentage: 68
Enter details for student 10:
Name: anil
Roll No: 10
Percentage: 58

Students ranked by percentage:


Rank: 1, Name: buddy, Roll No: 5, Percentage: 99.99
Rank: 2, Name: pawan, Roll No: 4, Percentage: 90.0
Rank: 3, Name: manish, Roll No: 1, Percentage: 89.0
Rank: 4, Name: dabbu, Roll No: 8, Percentage: 79.0
Rank: 5, Name: madhu, Roll No: 2, Percentage: 78.0
Rank: 6, Name: ravi, Roll No: 3, Percentage: 78.0
Rank: 7, Name: amar, Roll No: 7, Percentage: 69.0
Rank: 8, Name: ramesh, Roll No: 9, Percentage: 68.0
Rank: 9, Name: madhav, Roll No: 6, Percentage: 67.0
Rank: 10, Name: anil, Roll No: 10, Percentage: 58.0

9.WAP to overload “area()” method to compute area of circle, square,


rectangle and triangle

class Shape
{
public double area(double radius) {
return Math.PI * radius * radius;
}

public int area(int side) {


return side * side;
}

public int area(int length, int breadth) {


return length * breadth;
}

public double area(double base, double height, boolean isTriangle) {


return 0.5 * base * height;
}
}
class Area
{
public static void main(String[] args)
{
Shape s = new Shape();
System.out.println("Circle Area: " + s.area(5.0));
System.out.println("Square Area: " + s.area(4));
System.out.println("Rectangle Area: " + s.area(4, 6));
System.out.println("Triangle Area: " + s.area(4.0, 6.0, true));
}
}

output:
Circle Area: 78.53981633974483
Square Area: 16
Rectangle Area: 24
Triangle Area: 12.0

10.WAP to create a class “Number” consist of a number(int) and a static


variable count(int)
that count the number of instances created of the class and methods for
I/O and a static
method that print the value of count

class Number
{
private int number;
private static int count=0;

public Number(int n)
{
number=n;
count++;
}

public void displaynum()


{
System.out.println("number="+number);

}
public static void displaycount()
{
System.out.println("objected is created:"+count);
}
}

public class Numbertest


{
public static void main(String args[])
{
Number n1=new Number(10);
Number n2=new Number(20);
Number n3=new Number(30);
n1.displaynum();
n2.displaynum();
n3.displaynum();
Number.displaycount();

}
}
output:
number=10
number=20
number=30
objected is created:3

11.. WAP to create a class "Data" consist of two float type numbers and
methods for I/O. Create
an another class "Maximum" that inherits the "Data" class and consist of
a variable that
store the maximum of two inherited numbers and the methods for
I/O.(Single inheritance)

class Data
{
protected float x,y;

public void getdata(float a,float b)


{
x=a;
y=b;
}

public void showdata()


{
System.out.println("x="+x);
System.out.println("y="+y);
}
}
class Maximum extends Data
{
private float max;

public void max()


{
if(x>y)
{
max=x;
}
else
{
max=y;
}
}

public void showmax()


{
System.out.println("Maximum is:"+max);
}

}
public class Maximumtest
{
public static void main(String args[])
{
Maximum m=new Maximum();
m.getdata((float) 31.4, (float) 42.5);
m.showdata();
m.max();
m.showmax();
}
}

output:
x=31.4
y=42.5
Maximum is:42.5

12.WAP to create a class “Student” consist of name and rollno and methods
for I/O. Create
an another class “Test” that inherit the Student class and consist of
marks of two subjects
and methods for I/O. Create an another class “Result” that inherit the
Test class and consist
of total that stores the total marks of two subjects. Also override the
display() method in
Test and Result classes.(Multilevel inheritance/Method overriding)

class Student
{
private String name;
private int rollno;

public void getdata(String s,int r)


{
name=s;
rollno=r;
}
public void display()
{
System.out.println("Name:"+name);
System.out.println("Rollno:"+rollno);
}
}
class Marks extends Student
{
protected int marks1,marks2;

public void getmarks(int m1,int m2)


{
marks1=m1;
marks2=m2;
}

public void display()


{
super.display();
System.out.println("marks1:"+marks1);
System.out.println("marks2:"+marks2);
}
}

class Totalmarks extends Marks


{
private int total;

public void display()


{
super.display();
total=marks1+marks2;
System.out.println("Total marks is:"+total);
}
}

public class Resulttest


{
public static void main(String[] args)
{
Totalmarks t=new Totalmarks();
t.getdata("manish",123);
t.getmarks(80,56);
t.display();
}
}

output:
Name:manish
Rollno:123
marks1:80
marks2:56
Total marks is:136

13.WAP to create a class "Person" consist of name and age and a


parameterized constructor
to input the values and display() method. Create another two classes
"Teacher" and
"Student" that inherits the "Person " class and consist of post and
standard instance
variables respectively and methods to display the details. (Hierarchical
inheritance)

class Person
{
private String name; buddy
private int age;

public Person(String s,int a)


{
name=s;
age=a;
}

public void display()


{
System.out.println("name:"+name);
System.out.println("age:"+age);
}
}
class Teacher extends Person
{
private String post;

public Teacher(String s,int a,String c)


{
super(s,a);
post=c;
}
public void display()
{
super.display();
System.out.println("post:"+post);
}
}
class Student extends Person
{
private int standard;
public Student(String s,int a,int st)
{
super(s,a);
standard=st;
}
public void display()
{
super.display();
System.out.println("Standard:"+standard);
}
}

public class persontest


{
public static void main(String[] args)
{
Teacher t=new Teacher("manish",23,"TGT");
Student s=new Student("madhu",56,10);
t.display();
s.display();
}
}

output:
name:manish
age:23
post:TGT
name:madhu
age:56
Standard:10

You might also like