You are on page 1of 10

CS433P Programming Paradigm Lab

Register No:2160393
EXPERIMENT NO 1a
DATE :11-01-2023

Implementation of Simple Java programs to understand data types, variables,


operators, strings, input and output, control flow.

AIM: Write a java program implement simple calculator.

PROGRAM :
package com.mycompany.calculator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args)
{
int x,y,choice=0,result;
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x");
x = in.nextInt();
System.out.println("Enter the value of y");
y = in.nextInt();
while(choice!=5)
{
System.out.println("\n1.addition\n2.subtraction\n3.multiplication\n4.division");
choice = in.nextInt();
switch(choice) {
case 1:
result=x+y;`

Department of Computer Science and Engineering, Christ (Deemed to be 1


University) `
CS433P Programming Paradigm Lab

System.out.println("The sum of x and y is ="+result);


break;
case 2:
result =x -y;
System.out.println("The difference of x and y is ="+result);
break;
case 3:
result = x*y;
System.out.println("The product of x and y is ="+result);
break;
case 4:
result=x/y;
System.out.println("The divisor of x and y is ="+result);
break;
default
System.out.println("invalid");
}}}

Department of Computer Science and Engineering, Christ (Deemed to be 2


University) `
CS433P Programming Paradigm Lab

RESULT:

Department of Computer Science and Engineering, Christ (Deemed to be 3


University) `
CS433P Programming Paradigm Lab
Register No:2160393

EXPERIMENT NO 1b
DATE :11-01-2023

Implementation of Arrays in Java.

AIM: Write a java program to implement single dimensional array and sort using bubble sort.

PROGRAM:
package com.mycompany.lab1b;
import java.util.Scanner;
public class LAB1B {
public static void main(String[] args) {
int n,c,d,swap;
Scanner in =new Scanner(System.in);
System.out.println("The number of integers to sort");
n=in.nextInt();
int array[]=new int[n];
System.out.println("Enter "+n+" integers");
for(c=0;c<n;c++)
array[c]=in.nextInt();
for(c=0;c<n-1;c++){
for(d=0;d<n-c-1;d++){
if(array[d]>array[d+1]){
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;

Department of Computer Science and Engineering, Christ (Deemed to be 4


University) `
CS433P Programming Paradigm Lab
}
}
}
System.out.println("Sorted array is");
for(c=0;c<n;c++)
System.out.println(array[c]);
}
}

RESULT :

Department of Computer Science and Engineering, Christ (Deemed to be 5


University) `
CS433P Programming Paradigm Lab
Register No:2160393
EXPERIMENT NO 2
DATE :18-01-2023

Implementation of Classes and Objects – static fields, methods, method


parameters, object construction.

AIM: Write a program to create a class Distance with data members feet and inches.

Demonstrate the following concepts

Constructors without parameters and with passing parameters.

Method to set the distance values.

Method to add two distance objects by passing objects as arguments to methods.

To print the distances.

For Eg. : Adding two distances say 2' 11'' and 3' 3'' should give a value 6' 2''

PROGRAM:

package com.mycompany.pplab2;

import java.util.Scanner;

class Distance {

int feet;

int inches;

Distance()

feet=0;

Department of Computer Science and Engineering, Christ (Deemed to be 6


University) `
CS433P Programming Paradigm Lab
inches=0;

Distance(int f,int in)

feet=f;

inches=in;

void set(int fval,int ival)

fval=feet;

ival=inches;

void read()

Scanner sc=new Scanner(System.in);

System.out.println("enter feet");

feet=sc.nextInt();

System.out.println("enter inches");

inches=sc.nextInt();

void print()

System.out.println(feet+"/");

Department of Computer Science and Engineering, Christ (Deemed to be 7


University) `
CS433P Programming Paradigm Lab
System.out.println(inches+"//");

Distance adddistance(Distance num1,Distance num2)

Distance temp=new Distance();

temp.feet=num1.feet+num2.feet;

temp.inches=num1.inches+ num2.inches;

if(temp.inches>12)

temp.inches=temp.inches-12;

temp.feet=temp.feet+1;

return temp;

public class PPLAB2 {

public static void main(String[] args) {

Distance d=new Distance();

Distance d1=new Distance();

Distance d2=new Distance();

Distance sum=new Distance();

Department of Computer Science and Engineering, Christ (Deemed to be 8


University) `
CS433P Programming Paradigm Lab
d1.read();

d1.print();

d2.read();

d2.print();

sum=d.adddistance(d1,d2);

sum.print();

RESULT:

Department of Computer Science and Engineering, Christ (Deemed to be 9


University) `
CS433P Programming Paradigm Lab

Department of Computer Science and Engineering, Christ (Deemed to be


University) `

You might also like