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

Java Programming Experiments Overview

Uploaded by

pra.war.rt22
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)
7 views16 pages

Java Programming Experiments Overview

Uploaded by

pra.war.rt22
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

// Experiment 1

// Pratyush Warungase 22AM1031

import [Link];

public class Expt1

public static void main(String[] args)

int marks, choice;

Scanner input=new Scanner([Link]);

[Link]("Enter marks");

marks=[Link]();

choice=marks/10;

switch(choice)

case 1:

[Link]("fail");

break;

case 2:

[Link]("fail");

break;

case 3:

[Link]("fail");

break;

case 4:

[Link]("fail");

break;

case 5:

[Link]("D grade");

break;
case 6:

[Link]("C grade");

break;

case 7:

[Link]("B grade");

break;

case 8:

[Link]("A grade");

break;

case 9:

[Link]("A+ grade");

break;

case 10:

[Link]("fail");

break;

}
// Experiment 2

// Pratyush Warungase 22AM1031

public class Expt2

public static void main(String[]args)

int i,j,count;

for(i=1;i<=1000;i++)

count=0;

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

if(i%j==0)

count++;

if (count==2)

{[Link](i);

}
// Experiment 3

// Pratyush Warungase 22AM1031

void M1()

double area1=l*b;

[Link]("The area of the rectangle is: "+area1);

void M2()

double area2=s*s;

[Link]("The area of the square is: "+area2);

void M3()

double perimeter=2*(l+b);

[Link]("THe perimeter of the square is: "+perimeter);

class Expt3

public static void main (String[] args){

Scanner sc=new Scanner([Link]);

[Link]("Enter the length of rectangle: ");

double length=[Link]();

[Link]("Enter the breadth of the rectangle: ");

double breadth=[Link]();

[Link]("Enter the side of the square: ");

double side=[Link]();

area ob=new area();


ob.M0(length,breadth,side);

ob.M1();

ob.M2();

ob.M3();

}
// Experiment 4

// Pratyush Warungase 22AM1031

import [Link];

class Shape{

//Square

void area(float side){

[Link]("Area of Square="+side*side+"[Link]");

//Rectangle

void area(float length,float breadth){

[Link]("Area of Rectangle="+length*breadth+"[Link]");

//Circle

void area(double radius){

[Link]("Area of circle="+3.14*radius*radius+"[Link]");

//Triangle

void area(double base,double height){

[Link]("Area of triangle="+0.5*base*height+"[Link]");

class Expt4{

public static void main(String args[]){

Scanner sc=new Scanner([Link]);

[Link]("Enter the measure of the shapes:\n");

[Link]("Enter the side of the square: ");

float side=[Link]();

[Link]("Enter the length of rectangle: ");

float length=[Link]();
[Link]("Enter the breadth of rectangle: ");

float breadth=[Link]();

[Link]("Enter theradius of circle: ");

double radius=[Link]();

[Link]("Enter the base of Triangle: ");

double base=[Link]();

[Link]("Enter the height of Triangle: ");

double height=[Link]();

Shape Square=new Shape();

Shape Rectangle=new Shape();

Shape Circle=new Shape();

Shape Triangle=new Shape();

[Link](side);

[Link](length,breadth);

[Link](radius);

[Link](base,height);

}
// Experiment 5

// Pratyush Warungase 22AM1031

import [Link].*;

class Complex {

private double real;

private double imaginary;

public Complex() {

[Link] = 0.0;

[Link] = 0.0;

public Complex(double real, double imaginary) {

[Link] = real;

[Link] = imaginary;

public Complex add(Complex num) {

double sumReal = [Link] + [Link];

double sumImaginary = [Link] + [Link];

return new Complex(sumReal, sumImaginary);

public String toString() {

return "(" + real + " + " + imaginary + "i)";

}
public class Expt5 {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the real and imaginary parts of the first complex number:");

double real1 = [Link]();

double imaginary1 = [Link]();

Complex complex1 = new Complex(real1, imaginary1);

[Link]("Enter the real and imaginary parts of the second complex number:");

double real2 = [Link]();

double imaginary2 = [Link]();

Complex complex2 = new Complex(real2, imaginary2);

Complex sum = [Link](complex2);

[Link]("Sum of the complex numbers: " + sum);

[Link]();

}
// Experiment 6

// Pratyush Warungase

import [Link];
class Matrix {
int a[][], t[][];
int r, c, i, j;
void read() {
Scanner sc = new Scanner([Link]);
[Link]("Enter no. of rows and columns:");
r = [Link]();
c = [Link]();
a = new int[r][c];

[Link]("Enter First Matrix:");


for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
a[i][j] = [Link]();
}
}

[Link]();
}

void display() {
[Link]("Matrix:");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
[Link](a[i][j] + " ");
[Link]();
}
}
void transpose() {
t = new int[c][r];
for (i = 0; i < c; i++)
for (j = 0; j < r; j++)
t[i][j] = a[j][i];
[Link]("Transposed Matrix:");
for (i = 0; i < c; i++) {
for (j = 0; j < r; j++)
[Link](t[i][j] + " ");
[Link]();
}
}
}
class Expt6{
public static void main(String[] args)
{
Matrix ob = new Matrix();
[Link]();

[Link]();
[Link]();
}
}
// Experiment 7

// Pratyush 22AM1031

import [Link];

public class Expt7 {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String str = [Link]();

StringBuffer str1 = new StringBuffer(str);


[Link]();

String rev = [Link]();

if ([Link](rev)) {
[Link](str + " is a palindrome.");
} else {
[Link](str + " is not a palindrome.");
}

[Link]();
}
}

// Experiment 8
// Pratyush Warungase 22AM1031

import [Link];

class Circle {

protected double radius;

public void acceptRadius() {

Scanner sc = new Scanner([Link]);

[Link]("Enter the radius of the circle: ");

radius = [Link]();

class Area extends Circle {

private double area;

public void calculate() {

area = 3.14*radius*radius;

public void display() {

[Link]("Area of the circle is: " +area);

class Volume extends Area {

private double volume;

public void calculateVolume() {

volume = (4.0/3.0)*3.14*radius*radius*radius;

public void displayVolume() {

[Link]("Volume of the sphere is: " +volume);

public class Expt8 {

public static void main(String[] args) {


Volume sphere = new Volume();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

// Experiment 9
// Pratyush Warungase 22AM1031

import [Link];

class Student {

protected int roll_no;

public void getnumber() {

Scanner sc = new Scanner([Link]);

[Link]("Enter the roll number: ");

roll_no = [Link]();

public void putnumber() {

[Link]("Roll number: " +roll_no);

class Test extends Student {

protected float sem1, sem2;

public void getmarks() {

Scanner sc = new Scanner([Link]);

[Link]("Enter the marks for semester 1: ");

sem1 = [Link]();

[Link]("Enter the marks for semester 2: ");

sem2 = [Link]();

public void putmarks() {

[Link]("Semester 1 Marks: " +sem1);

[Link]("Semester 2 Marks: " +sem2);

interface Sports {

float score = 0.0f;


void putscore();

class Result extends Test implements Sports {

private float total;

public void display() {

total = sem1+sem2+score;

putnumber();

putmarks();

putscore();

[Link]("Total Score: " +total);

public void putscore() {

[Link]("Sports Score: " +score);

public class Expt9 {

public static void main(String[] args) {

Result sr = new Result();

[Link]();

[Link]();

[Link]();

Common questions

Powered by AI

In Experiment 9, the Result class implements the Sports interface, weaving additional responsibilities (sports scoring) into the class beyond inherited academic scoring. However, given the absence of method implementation beyond the putscore(), the interface's potential for contract-driven design seems underutilized, barely extending functionality or imposing behavioral outlines on Result, prompting a re-evaluation of its necessity or implementation to enhance effectiveness in segregated concerns and enhanced modular class structure.

The algorithm in Experiment 2 is inefficient for finding prime numbers as it checks all potential divisors up to the number itself (i), leading to O(n²) complexity for finding primes up to n. A more efficient approach would use the Sieve of Eratosthenes or iterate only up to the square root of i, reducing unnecessary checks by eliminating non-factors earlier in the loop.

The palindrome check in Experiment 7 ascertains a string's palindrome status by reversing the input via StringBuffer and comparing to the original. This approach is simple but not optimal for large strings due to additional space and time complexity incurred by full reversal and string manipulation operations. Direct character comparison from opposite ends towards the center could enhance efficiency by bypassing entire data duplication and focusing solely on significant palindromic verification.

In Experiment 8, the class Volume inherits from Area, which in turn extends Circle. This chain demonstrates inheritance as it allows Volume to access and build upon the properties and methods of Area and Circle. Such a structure permits code reusability and extensibility; for instance, Volume can calculate both the area and volume using the inherited radius, showcasing hierarchical relationships between geometric calculations.

Experiment 6 implicitly shows benefits of nesting via its contained structure (Matrix methods under main class) similar to nested class benefits like organizational clarity, encapsulation, and logical grouping when implementing operations like reading, displaying, and transposing matrices. Though not explicitly nested, keeping related functionality within a single thematic class aids in maintaining cohesion and specialized behavior functionality, minimizing external interference by conceiving cohesive unit modules internally manageable.

If the code in Experiment 3 attempts to call the method for calculating the perimeter of a square, it will encounter a conceptual error because the method M3() incorrectly references calculating the perimeter of a square while the formula used within is for a rectangle's perimeter (2*(l+b)). This inconsistency signifies a misunderstanding or mislabeling error within the code's comments or logic.

In Experiment 4, method overloading allows the 'area' method to be implemented multiple times with different parameter lists to calculate areas for various shapes—square, rectangle, circle, and triangle. Each variant of the method performs calculations appropriate for a specific shape based on the parameters provided (e.g., side for square, length and breadth for rectangle), promoting code reusability and clarity while encapsulating shape-specific logic within the same method name.

Experiment 1 uses a switch-case structure to classify grades based on the marks divided by ten. This means it categorizes ranges, e.g., marks 90-99 map to choice 9 (A+ grade). The case for a perfect score (marks = 100) incorrectly maps to '10', leading to 'fail'. The logic flaw misplaces the grade because it doesn't account for the perfect score, reflecting a need for a separate case or adjustment for this specific maximum edge case.

The Complex class in Experiment 5 demonstrates encapsulation by using private data members (real and imaginary) and public methods for operations like addition. Encapsulation ensures that the internal representation is hidden from outside interference or misuse, as direct access to the class's internal state is not permitted. This approach enhances data integrity and abstraction, allowing changes to the Complex class implementation without affecting external code using this class.

Experiment 4 employs polymorphism through method overloading within the Shape class to perform area calculations for different shapes. Each overloaded 'area' method represents a polymorphic behavior as the same method name adapts functionality based on input parameters, offering flexibility and streamlined interaction for calculating diverse shape areas under a unified method header. This polymorphic approach elegantly manages multiple distinct operations logically related by area calculation.

You might also like