0% found this document useful (0 votes)
14 views16 pages

Java Lab

java programming lab report

Uploaded by

pranshu1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Java Lab

java programming lab report

Uploaded by

pranshu1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Experiment No.

1
Aim:
To write Java programs for:

1. Printing a name
2. Performing addition, subtraction, multiplication, and division using:
o Variables
o Scanner class
o Command Prompt arguments

Objective:

 Learn basic Java syntax and structure.


 Learn variable declaration and arithmetic operations.
 Learn to take input from the user via Scanner.
 Learn command-line arguments usage.

Code:

Program 1: Print Name

class Main {
public static void main(String[] args) {
System.out.println("My name is Pranshu Tripathi");
}
}
Program 2a: Arithmetic using variables

public class ArithmeticVariables {


public static void main(String[] args) {
int a = 20;
int b = 10;

System.out.println("Addition = " + (a + b));


System.out.println("Subtraction = " + (a - b));
System.out.println("Multiplication = " + (a * b));
System.out.println("Division = " + (a / b));
}
}

Program 2b: Arithmetic using Scanner

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int addition, subtraction, multiplication;
float division;

addition = a + b;
subtraction = a - b;
multiplication = a * b;
division = (float) a / (float) b;

System.out.println("Addition = " + addition + ", Subtraction = " + subtraction


+ ", Multiplication = " + multiplication + ", Division = " + division);
System.out.println("Pranshu Tripathi");

sc.close();
}
}

Program 2c: Arithmetic using Command Line arguments

public class ArithmeticCommandLine {


public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

System.out.println("Addition = " + (a + b));


System.out.println("Subtraction = " + (a - b));
System.out.println("Multiplication = " + (a * b));
System.out.println("Division = " + (a / b));
}
}

Output (Command Prompt style):


Experiment No. 2
Aim:
To write Java programs for:

1. Calculating factorial of a number using Scanner


2. Taking 5 numbers from user and calculating:
o Average
o Maximum and minimum
3. Performing arithmetic operation (+,-,*,/,%) using Switch Case

Objective:

 Learn loops, arrays, and decision making using switch-case.

Code:

Program 1: Factorial

import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int fact = 1;
for(int i=1; i<=n; i++){
fact *= i;
}
System.out.println("Factorial of " + n + " = " + fact);
sc.close();
}
}
Program 2: Average, Maximum, Minimum

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
public s import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();

float average = (float)(a+b+c+d+e)/5;


int max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));
int min = Math.min(a, Math.min(b, Math.min(c, Math.min(d, e))));

System.out.println("Average = " + average + ", Maximum = " + max + ", Minimum = "
+ min);
System.out.println("Pranshu Tripathi");
}
}tatic void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();

float average = (float)(a+b+c+d+e)/5;


int max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));
int min = Math.min(a, Math.min(b, Math.min(c, Math.min(d, e))));

System.out.println("Average = " + average + ", Maximum = " + max + ", Minimum = "
+ min);
System.out.println("Pranshu Tripathi");
}
}

Program 3: Switch Case Arithmetic

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
char op = sc.next().charAt(0);

switch(op) {
case '+':
System.out.println("Result = " + (a + b));
break;

case '-':
System.out.println("Result = " + (a - b));
break;

case '*':
System.out.println("Result = " + (a * b));
break;

case '/':
if (b != 0) {
System.out.println("Result = " + ((float) a / b));
} else {
System.out.println("Error: Division by zero");
}
break;

default:
System.out.println("Invalid operator.");
}
sc.close();
System.out.println("Pranshu Tripathi");
}
}
}

Output:
Experiment No. 3
Aim:
To write Java programs for:

1. Reversing an integer
2. Finding largest element in array using sort
3. Multiplying two arrays
4. String methods (equals, reverse, change case)
5. Checking if a substring exists

Objective:

 Learn array operations, loops, and string manipulations.

Code:

Program 1: Reverse Number

import java.util.Scanner;

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = sc.nextInt();
int rev = 0;
while(num != 0){
rev = rev*10 + num%10;
num /= 10;
}
System.out.println("Reversed number = " + rev);
sc.close();
}
}

Program 2: Largest element using sort

import java.util.Arrays;

public class LargestElement {


public static void main(String[] args) {
int[] arr = {10, 50, 20, 70, 30};
Arrays.sort(arr);
System.out.println("Largest element = " + arr[arr.length-1]);
}
}

Program 3: Multiply two arrays

public class MultiplyArrays {


public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = new int[a.length];
for(int i=0;i<a.length;i++){
c[i] = a[i]*b[i];
}
System.out.print("Result array: ");
for(int i=0;i<c.length;i++) System.out.print(c[i]+" ");
}
}

Program 4: String methods

import java.util.Scanner;

public class StringMethods {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String s1 = sc.nextLine();
System.out.print("Enter second string: ");
String s2 = sc.nextLine();

System.out.println("Strings equal? " + s1.equals(s2));

String rev = new StringBuilder(s1).reverse().toString();


System.out.println("Reverse of first string: " + rev);

System.out.println("Upper case: " + s1.toUpperCase());


System.out.println("Lower case: " + s1.toLowerCase());

System.out.print("Enter substring to search: ");


String sub = sc.nextLine();
System.out.println("Substring exists? " + s1.contains(sub));
sc.close();
}
}

Output:
Experiment No. 4
Aim:
To write Java programs for:

1. Circle class (area and circumference)


2. Rectangle class (area and perimeter)
3. Shape class with multiple constructors (Rectangle, Circle, Square)

Objective:

 Learn classes, objects, parameterized constructors, and method definition.

Code:

Program 1: Circle class

class Circle {
double radius;
Circle(double r){ radius = r;}
double area(){ return 3.14*radius*radius;}
double circumference(){ return 2*3.14*radius;}
}

public class CircleTest {


public static void main(String[] args){
Circle c1 = new Circle(5);
Circle c2 = new Circle(10);
System.out.println("Area c1 = "+c1.area()+" Circumference c1 = "+c1.circumference());
System.out.println("Area c2 = "+c2.area()+" Circumference c2 = "+c2.circumference());
}
}

Program 2: Rectangle class

class Rectangle {
double length, width;
Rectangle(double l,double w){ length=l; width=w;}
double area(){ return length*width;}
double perimeter(){ return 2*(length+width);}
}

public class RectangleTest {


public static void main(String[] args){
Rectangle r1 = new Rectangle(5,10);
Rectangle r2 = new Rectangle(7,3);
System.out.println("Area r1 = "+r1.area()+" Perimeter r1 = "+r1.perimeter());
System.out.println("Area r2 = "+r2.area()+" Perimeter r2 = "+r2.perimeter());
}
}

Program 3: Shape class


class Shape {
Shape(int l, int w){ System.out.println("Rectangle with l="+l+" w="+w);}
Shape(double r){ System.out.println("Circle with r="+r);}
Shape(int s){ System.out.println("Square with side="+s);}
}

public class ShapeTest {


public static void main(String[] args){
Shape s1 = new Shape(10,20);
Shape s2 = new Shape(20.5);
Shape s3 = new Shape(10);
}
}
Output:

Experiment No. 5
Aim:

To write Java programs for:

1. MyNumber class with methods (isPositive, isNegative, etc.)


2. Using this keyword for method and constructor invocation
3. Student class with object count using static member

Objective:

 Learn this keyword usage.


 Learn static members and object counting.
 Practice OOP concepts and constructors.

Code:

Program 1: MyNumber class

class MyNumber {
private int num;
MyNumber(){ num=0;}
MyNumber(int n){ this.num = n;}
boolean isNegative(){ return num<0;}
boolean isPositive(){ return num>0;}
boolean isZero(){ return num==0;}
boolean isOdd(){ return num%2!=0;}
boolean isEven(){ return num%2==0;}
}
public class MyNumberTest {
public static void main(String[] args){
MyNumber n1 = new MyNumber();
MyNumber n2 = new MyNumber(15);
System.out.println("n2 is positive? "+n2.isPositive());
System.out.println("n2 is odd? "+n2.isOdd());
}
}

Program 2: this.display()

class TestThis {
void display(){ System.out.println("Display method called"); }
void callDisplay(){ this.display(); }
public static void main(String[] args){
TestThis t = new TestThis();
t.callDisplay();
}
}

Program 3: this() constructor

class ConstructorThis {
ConstructorThis(){ System.out.println("Default constructor"); }
ConstructorThis(int x){ this(); System.out.println("Parameterized constructor with x="+x);
}
public static void main(String[] args){ ConstructorThis obj = new ConstructorThis(10); }

}
Program 4: Student class with object count

class Student {
int roll;
String name;
double percent;
static int count = 0;

Student(int r, String n, double p){


roll = r; name = n; percent = p;
count++;
System.out.println("Object "+count+" created: "+name+" Roll: "+roll+" %: "+percent);
}
}

public class StudentTest {


public static void main(String[] args){
Student s1 = new Student(1,"Alice",85.5);
Student s2 = new Student(2,"Bob",90.0);
Student s3 = new Student(3,"Charlie",78.0);
System.out.println("Total objects created: "+Student.count);
}
}
Output:

You might also like