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

Java Programs for Basic Arithmetic Operations

Uploaded by

ANJALI MODI
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 views37 pages

Java Programs for Basic Arithmetic Operations

Uploaded by

ANJALI MODI
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

/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name: Addition of two numbers

***********************************/

class Add

public static void main(String args[])

int x,y,z;

x=56;

y=44;

z=x+y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Subtraction of two numbers

***********************************/

class Sub

public static void main(String args[])

int x,y,z;

x=25;

y=15;

z=x-y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Multiplication of two numbers

***********************************/

class multi

public static void main(String args[])

int x,y,z;

x=56;

y=44;

z=x*y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Division of two numbers

***********************************/

class Div

public static void main(String args[])

int x,y,z;

x=99;

y=3;

z=x/y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Performing Arithmatic operations

***********************************/

class Arith

public static void main(String args[])

int x,y,a,b,c,d;

x=99;

y=3;

a=99+3;

b=99-3;

c=99*3;

d=99/3;

[Link](a);

[Link](b);

[Link](c);

[Link](d);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Area of Circle

***********************************/

class Area

public static void main(String args[])

int r;

double pi,a;

r=2;

pi=3.14;

a=pi*r*r;

[Link](a);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Equation of straight lines

***********************************/

class equation

public static void main(String args[])

double y,m,x,c;

m=2;

x=3;

c=1;

y=m*x+c;

[Link](y);

OUTPUT :
/*************************************
Name-Bhuvi Jain
Batch-A1
Enrollment No.- 220158
Aim- To create a menu
**************************************/
class Menu
{
public static void main(String args[])
{
int v=2,w=1,x=4,y=3,z=1;
[Link]("Hostel Facilities");
switch(v)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Mess Facilities");
switch(w)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Hospital Facilities");
switch(x)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Lab Facilities");
switch(y)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Shuttle Facilities");
switch(z)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
}
}
OUTPUT :
/*************************************
Name-Bhuvi Jain
Batch-A1
Enrollment No.- 220158

Aim- To print grades A,B,C,D,E for given marks

**************************************/

class Grades{

public static void main(String args[]){

double marks=65;

if(marks<100 && marks>=90)

{[Link]("A");

else if(marks>=80 && marks<90)

{[Link]("B");

else if(marks<80 && marks>=70)

{[Link]("C");

else if(marks<70 && marks>=60)

{[Link]("D");

else

{[Link]("E");

OUTPUT:
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Adding two times where each time is described with three variables hour minute and
second.

***********************************/

class Timetest2

int h,m,s;

void inpt(int a,int b,int c)

h=a;

m=b;

s=c;

void outpt()

[Link]("h="+h+"m="+m+"s="+s);

void timeadd(Timetest2 t1,Timetest2 t2)

h=t1.h+t2.h;

m=t1.m+t2.m;

s=t1.s+t2.s;

if(s>59)

s=s-60;

m=m+1;

if(m>59)
{

m=m-60;

h=h+1;

class Test2

public static void main(String args[])

Timetest2 O1=new Timetest2();

Timetest2 O2=new Timetest2();

Timetest2 O3=new Timetest2();

[Link](17,18,23);

[Link](10,58,10);

[Link](O1,O2);

[Link]();

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Adding two distances where each distance is described with three variables
m,cm,mm.

***********************************/

class Timetest3

int m,cm,mm;

void inpt(int a,int b,int c)1

m=a;

cm=b;

mm=c;

void outpt()

[Link]("m="+m+"cm="+cm+"mm="+mm);

void timeadd(Timetest3 t1,Timetest3 t2)

m=t1.m+t2.m;

cm=[Link]+[Link];

mm=[Link]+[Link];

if(mm>9)

mm=mm-10;

cm=cm+1;

if(cm>99)
{

cm=cm-100;

m=m+1;

class Test3

public static void main(String args[])

Timetest3 O1=new Timetest3();

Timetest3 O2=new Timetest3();

Timetest3 O3=new Timetest3();

[Link](17,80,30);

[Link](10,58,10);

[Link](O1,O2);

[Link]();

}
OUTPUT:
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the double of 1-D array and test it using main function.

***********************************/

CODE :

import [Link].*;

class double1Dmain

public static void main(String args[])

double1D t1= new double1D();

[Link]();

[Link]();

[Link]();

class double1D

int a[];

double1D()

a=new int[5];

void input()

[Link]("enter the array elements");

Scanner sc=new Scanner([Link]);

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

{
a[i]=[Link]();

void output()

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

[Link](a[i]);

void proc()

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

a[i]=2*a[i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the sum of 1-D array and using objects and classes.

***********************************/

Code:
import [Link].*;

class sum1Dmain

public static void main(String args[])

sum1D t1= new sum1D();

[Link]();

[Link]();

[Link]();

class sum1D

int a[];

int sum=0;

sum1D()

a=new int[5];

void input()

[Link]("enter the array elements");

Scanner sc=new Scanner([Link]);

for(int i=0;i<5;i++)
{

a[i]=[Link]();

void output()

[Link](sum);

void proc()

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

sum=sum+a[i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the reverse of 1-D array using 2nd array.

***********************************/

Code :

import [Link].*;

class ReverseMain {

public static void main(String[] args) {

Reverse t1 = new Reverse();

[Link]();

[Link]();

[Link]();

class Reverse

int a[];

int b[];

Reverse(){

a=new int[5];

b=new int[5];

void input()

[Link]("enter the elements:");

Scanner sc= new Scanner([Link]);

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

a[i]=[Link]();
}

void output(){

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

[Link](b[i]+" ");

void proc()

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

b[i]=a[4-i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Program to do the sum of two matrices of size 3x3 using object and classes

***********************************/

Code:

import [Link].*;

class Matrixmain {

public static void main(String[] args)

Matrix t1 = new Matrix();

[Link]();

[Link]();

[Link]();

class Matrix {

int a[][];

int b[][];

int c[][];

Matrix() {

a = new int[3][3];

b = new int[3][3];

c = new int[3][3];

void input() {

[Link]("Enter the array elements:");

Scanner sc = new Scanner([Link]);

[Link]("Enter the elements of array a:");

for (int i = 0; i < 3; i++) {


for(int j=0;j<3;j++){

a[i][j] = [Link](); } }

[Link]("Enter the elements of array b:");

for (int i = 0; i < 3; i++) {

for(int j=0;j<3;j++){

b[i][j] = [Link](); } }

void output() {

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

{ for(int j=0;j<3;j++)

{[Link](c[i][j]+" ");

[Link](); } }

void proc() {

for (int i = 0; i < 3; i++) {

for(int j=0;j<3;j++)

c[i][j]= a[i][j]+b[i][j];

}}

OUTPUT :
/*******************************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Wite a program using this() keyword.

*********************************************/

Code :

import [Link].*;

class Test

int x,y;

Test()

Test(int x,int y)

this.x=x;

this.y=y;

void output()

[Link]("value of x : " + x);

[Link]("value of y : " + y);

public class Testmain {

public static void main(String args[])

Test t1=new Test(5,7);

[Link](); }

}
OUTPUT :
/*******************************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Wite a program using Super keyword.

*********************************************/

Code :
class Vehicle{
String name="Bus";}
class Bus extends Vehicle{
String name="School Bus";
void printName(){
[Link]("Class Bus:");
[Link](name);
[Link]("Class Vehicle:");
[Link]([Link]);}}
public class TestSuper1 {
public static void main(String[] args) {
// TODO code application logic here
Bus d=new Bus();
[Link]();
}}
OUTPUT:
/***********************************
NAME:Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name:Adding two times where each time is described with two variables hour and
minute
***********************************/
class Timetest2
{
int h,m;
void inpt(int a,int b)
{
h=a;
m=b;
}
void outpt()
{
[Link]("h="+h+"m="+m);
}
void timeadd(Timetest2 t1,Timetest2 t2)
{
h=t1.h+t2.h;
m=t1.m+t2.m;
if(m>59)
{
m=m-60;
h=h+1;
}
}
}
class Test2
{
public static void main(String args[])
{
Timetest2 O1=new Timetest2();
Timetest2 O2=new Timetest2();
Timetest2 O3=new Timetest2();
[Link](17,18)
[Link](10,58);
[Link](O1,O2);
[Link]();
}
}
OUTPUT :
/**********************************************
NAME: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: Create a package for printing Hello World
************************************************/
Creating package
package xyz;
public class Hello
{
public void printhello()
{
[Link]("Hello World");
}
}
Importing Package
import xyz .*;
class Test
{
public static void main(String args[])
{
Hello t1= new Hello();
[Link]();
}
}
/************************************************************************
NAME: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. No.:220158
Program Name: Create package for any 5 created programs and test function and methods
**************************************************************************/
Creating Package
(i)package calculation;
public class Add
{
public void Add()

{
int x,y,z;
x=56;
y=44;
z=x+y;
[Link](z);
}
}
(ii) package calculation;
public class Area
{
public void Area()
{
int r;
double pi,a;
r=2;
pi=3.14;
a=pi*r*r;
[Link](a);
}
}
(iii) package calculation;
public class Div
{
public void Div()
{
int x,y,z;
x=99;
y=3;
z=x/y;
[Link](z);
}
}
(iv) package calculation;
public class multi
{
public void multi()
{
int x,y,z;
x=56;
y=44;
z=x*y;
[Link](z);
}
}
(v) package calculation;
public class Sub
{
public void Sub()
{
int x,y,z;
x=25;
y=15;
z=x-y;
[Link](z);
}
}
Importing Package
import calculation.*;
class arithmetic
{
public static void main(String args[])
{
Add t1=new Add();
Area t2=new Area();
Div t3=new Div();
multi t4 =new multi();
Sub t5=new Sub();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
/****************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: MultiThreading
*****************************/
class test1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun1->"+i);
}}
}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun2->"+i);
}}
}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
[Link]();
[Link]();
}}
OUTPUT:
/**************************************************************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: To set the priority of threads by different values and analyze the output.
**************************************************************************/
class test1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun1->"+i);
} }}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun2->"+i);
} }}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
[Link](5);
[Link](6);
[Link]();
[Link]();}}
OUTPUT
/**************************************************************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: Write a program to test different methods of thread.
isalive(),join(),sleep(), setname(),getname()

**************************************************************************/
Code:
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + ": " + i);
try {
[Link](500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class ThreadMethodsDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
[Link]("Thread 1 is alive before start: " + [Link]()); // false
[Link]();
[Link]("Thread 1 is alive after start: " + [Link]()); // true
MyThread thread2 = new MyThread();
[Link]("Main thread starts thread2 and waits for it to finish...");
[Link]();
try {
[Link](); // Main thread waits for thread2 to finish
} catch (InterruptedException e) {
[Link](e);
}
[Link]("Main thread resumes after thread2 finishes.");
[Link]("Main thread sleeping for 2 seconds...");
try {
[Link](2000); // Main thread sleeps for 2 seconds
} catch (InterruptedException e) {
[Link](e);
}
[Link]("Main thread woke up after sleeping.");
MyThread thread3 = new MyThread();
[Link]("CustomThreadName");
[Link]("Thread 3 name: " + [Link]());
}
}
OUTPUT:

Common questions

Powered by AI

The 'this' keyword is used in object-oriented programming to refer to the current instance of a class. It allows an object to refer to itself and distinguish between class attributes and parameters. In the provided example, 'this' is utilized in constructors of the 'Test' class to assign values to instance variables x and y. By doing so, it helps in differentiating between the instance variables (this.x, this.y) and the constructor parameters (x, y).

The program manages the addition of two times by first instantiating two objects, 'O1' and 'O2', of the 'Timetest2' class. Each object represents a time with hours, minutes, and seconds. The 'timeadd()' method is then called on a third object 'O3' which sums the hours, minutes, and seconds of 'O1' and 'O2'. It adjusts for any overflow in seconds by converting excess into minutes, and similarly, it adjusts for overflow in minutes by converting excess into hours. This method ensures the resulting time is correctly formatted .

The code illustrates basic multithreading by creating two threads using classes extending Thread: 'test1' and 'test2'. Each thread has its own run method that prints numbers. The main method sets the priority of 'test1' and 'test2' to 5 and 6, respectively, then starts both threads. Thread priority hints to the operating system about the order of execution, though it is not guaranteed. Higher priority threads like 'test2' are likely intended to get more CPU time and run first, though actual execution can vary due to the system's thread scheduling policy. The impact is often more visible in more complex programs .

The code creates different classes for each arithmetic operation, such as addition, subtraction, multiplication, and division. Each class initializes variables, performs an operation, and outputs the result. For instance, in the 'Add' class, variables x and y are initialized to 56 and 44 respectively, and their sum, z, is computed and printed. The implication on variable management is that each operation requires re-declaration of variables within its context, which could lead to inefficient handling in larger programs and lack of modularity for common operations across different classes .

The method to add distances in meters, centimeters, and millimeters is implemented by aligning these units to effectively sum them while carrying excess units to the next higher order. Nevertheless, this approach could be further improved by encapsulating conversion operations into separate helper methods to enhance readability and reusability. Furthermore, incorporating validation checks for input correctness and edge handling for negative values can enhance robustness. Another improvement involves leveraging classes to encapsulate each metric unit and using encapsulated methods to manage conversions .

The provided examples do not explicitly employ interfaces but demonstrate class inheritance through the use of the 'super' keyword. In the Vehicle-Bus example, 'Bus' inherits properties from 'Vehicle', where 'Bus' can override the name attribute while still accessing the parent class attribute through 'super.name'. This demonstrates polymorphism, where subclasses retain methods of the parent class but can also modify or extend behavior. Using interfaces and inheritance allows for reuse of code by providing common functionality across multiple classes while allowing extensions and modifications that make code more modular and flexible .

To optimize the doubling of elements in a 1-D array for larger datasets, one could utilize parallel streams or the Fork-Join framework in Java for concurrent processing, which takes advantage of multicore processors to improve performance. Enhancements might include initializing arrays with specific sizes to reduce memory overhead and reduce system resource usage. These techniques help in efficient management of system resources when dealing with extensive datasets. Optimizing such processes would ensure scalability and performance .

Implementing a time addition program requires consideration of time zones, daylight saving adjustments, and AM/PM distinctions for 12-hour formats. It's important to ensure that the time calculations handle leap years and leap seconds if date computations are involved. For applications, libraries like Java's DateTime should be used to handle complexities and provide functionality like international time zone adjustments and format conversions effectively. Ensuring accuracy in arithmetic computations allows such programs to be used reliably in applications like scheduling systems .

In the document, individual classes are created for each mathematical operation, such as addition and multiplication, which are then grouped into a single package 'calculation'. Utilizing packages allows for better organization, modularity, and namespace management of classes, enabling easier maintenance and scalability in programming, as evident in importing and utilizing these packaged classes in an 'arithmetic' program. In contrast, individual classes without packaging may lead to cluttered codebases, especially in larger projects where similar class names might lead to conflicts .

Challenges in the matrix summation program might include ensuring input correctness, handling exceptions like input mismatches, and ensuring correct array indexing, particularly in a 3x3 matrix. These can be addressed by validating inputs before processing, using try-catch blocks to handle incorrect inputs or handling ArrayIndexOutOfBoundsException, and implementing checks to ensure correct matrix dimension matching before performing operations. Additionally, using arrays more dynamically with ArrayLists or matrices with generic sizes can improve flexibility .

You might also like