23BCP420 Java Practical File
23BCP420 Java Practical File
Raisan,Gandhinagar-382426,Gujarat,India
JAVA LAB
class first {
}}OUTPUT
2. Write a Java program to print the sum of two numbers.
public class lab2b{
public static void main(String[] args){
int a=10,b=20;
int c=a+b;
System.out.println("the sum is "+c);
}
}
OUTPUT
LAB-2
1. You are developing a mathematical tool that requires generating a list of prime
numbers. How would you implement a Java program to generate the first n
prime numbers?
import java.util.*;
public class prime{
public static void main(String args[]){
Scanner ab=new Scanner(System.in);
System.out.print(" Enter any number");
int n=sc.nextInt();
int j=2; int i=1;
while(i<=n){
int count=0;
for(int k=1; k<=j;k++){
if(j%k==0){
count++;
}
}
if(count==2){
System.out.println(j);
i++;}
j++;
}
}
}
OUTPUT
2. Write a program to enter two numbers and perform mathematical operations
on them.
import java.util.*;
class operation{
public static void main(String[] args){
Scanner scanner= new Scanner(System.in);
System.out.println("Enter the first number");
int a= scanner.nextInt();
System.out.println("Enter the second number");
int b= scanner.nextInt();
switch (choice) {
case 1:
int add=a+b;
System.out.println("the sum is\t"+add);
break;
case 2:
int sub=a-b;
System.out.println("the subraction is"+sub);
break;
case 3:
int mul=a*b;
System.out.println("the multipication is"+mul);
break;
case 4:
int div=a/b;
System.out.println("the division is"+div);
break;
default:
int max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ?
num2 : num3);
scanner.close();
}
}
OUTPUT
4. You're working on a text analysis feature that counts the number of vowels
and consonants in a given line of text. Write a program to accept a line and
check how many consonants and vowels are there in line.
import java.util.*;
public class text{
public static void main(String args[]){
Scanner ab=new Scanner(System.in);
System.out.print("Enter any string\n");
String str=ab.next();
str.toLowerCase();
int v_count=0,c_count=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch=='a'|| ch=='e'||ch=='i'|| ch=='o'|| ch=='u'){ v_count++;
}
else{ c_count++;
}
}
System.out.println("vowels = " +v_count+" and consonats = "+c_count);
}
}
OUTPUT
5. Write an interactive program to print a string entered in a pyramid form.
For instance, the string “stream” has to be displayed as follows:
S
S t
Str
Stre
S t r e am
return max;
}
}
OUTPUT
7. Write a java program to perform addition and multiplication of Two
Matrices.
import java.util.Scanner;
scanner.close();
}
//23BCP420
class Distance {
private int feet;
private int inches;
//23BCP420
import java.util.*;
class Distance{
public static void main(String args[]){ Dist d1=new Dist(6,9);
d1.display();
Dist d2=new Dist(); d2.display();
System.out.println(); Dist d3=d2;
d3.display();
d2.inch=4;
d3.display();
}
}
class Dist{
int feet,inch; Dist(){
feet=9; inch=4;
}
Dist(int a,int b){ this.feet=a;
this.inch=b;
}
public void display(){
System.out.println(" Distance in inch and feet is "+inch+ " and "+feet);
}
}
OUTPUT
3. Write a program to show the difference between public and private access
specifiers. The program should also show that primitive data types are passed by
value and objects are passed by reference and to learn use of final keyword.
OUTPUT
// after changing password to public type
OUTPUT
4. Write a program that implements two constructors in the class. We call the
other constructor using ‘this’ pointer, from the default constructor of the class.
mathStudent.displayInfo();
mathStudent.displayMathMarks();
scienceStudent.displayInfo();
scienceStudent.displayScienceMarks();
artStudent.displayInfo(); artStudent.displayArtMarks();
}
class Student {
void displayInfo() {
System.out.println("This is a student.");
}
}
class MathStudent extends Student { void displayMathMarks() {
System.out.println("Math Student marks: 85");
}
}
class ScienceStudent extends Student { void displayScienceMarks() {
System.out.println("Science Student marks: 90");
}
}
class ArtStudent extends Student { void displayArtMarks() {
System.out.println("Art Student marks: 75");
}
}OUTPUT
LAB 5
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class Polymorphism
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
OUTPUT
ii. Write a program to compute if one string is a rotation of another. For example,
pit is rotation of tip as pit has same character as tip.
// Check if s2 is a rotation of s1
int n = s1.length();
for (int i = 0; i < n; i++) {
// Create a rotated version of s1
String rotated = s1.substring(i) + s1.substring(0, i);
if (rotated.equals(s2)) {
return true;
}
}
return false;
}
}
OUTPUT
LAB 6
6. Study and implement Abstract class and Interfaces in Java i.
Describe abstract class called Shape which has three subclasses
say Triangle, Rectangle, Circle. Define one method area() in the
abstract class and override this area() in these three subclasses
to calculate for specific object i.e. area() of Triangle subclass
should calculate area of triangle etc. Same for Rectangle and
Circle.
class AbstractClassEx
{
public static void main(String arg[])
{
Triangle t=new Triangle(4.3f,5.3f);
Rectangle r=new Rectangle(2.4f,4.2f);
Circle c=new Circle(10.5f);
System.out.println(t.area());
System.out.println(r.area());
System.out.println(c.area());
}
}
OUTPUT
ii. Write a Java program to create an abstract class Employee
with abstract methods calculateSalary() and displayInfo().
Create subclasses Manager and Programmer that extend the
Employee class and implement the respective methods to
calculate salary and display information for each role.
public class Company{
public static void main(String[] args) {
System.out.println("Manager Information:");
manager.displayInfo();
System.out.println();
System.out.println("Programmer Information:");
programmer.displayInfo();
}
}
double calculateSalary() {
return baseSalary + bonus;
}
void displayInfo() {
System.out.println("Manager's Salary: " + calculateSalary());
System.out.println("Base Salary: " + baseSalary);
System.out.println("Bonus: " + bonus);
}
}class Programmer extends Employee {
private double baseSalary;
private int numberOfProjects;
double calculateSalary() {
return baseSalary + (numberOfProjects * 1000);
}
void displayInfo() {
System.out.println("Programmer's Salary: " + calculateSalary());
System.out.println("Base Salary: " + baseSalary);
System.out.println("Number of Projects: " + numberOfProjects);
}
}
OUTPUT
iii. Write a Java program to create an interface Shape with the
getArea() method. Create three classes Rectangle, Circle, and
Triangle that implement the Shape interface. Implement the
getArea() method for each of the three classes.
interface Shape {
double getArea();
}
OUTPUT