0% found this document useful (0 votes)
63 views12 pages

Tesktrpk$d

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

Practical 1

Aim: Write a java program for converting Pound into Rupees. (Accept Pounds from command line
argument and using scanner class also and take 1 Pound = 100 Rupees.)

Code:

import java.util.*;
class Cons{ ––
double r;
int p1=23;
public Cons(){
r=93.32*p1;
}
public Cons( int p){
r=93.32*p;
}

public static void main(String args[]){


Cons c1= new Cons();
Cons c2 = new Cons(10);
System.out.println(" enter ruppes " );
Scanner sc=new Scanner(System.in);
c1.p1= sc.nextInt();

System.out.println(" converted into ruppe by command line argument " + c1.r);

System.out.println(" converted into ruppe by parameterized constructor " + c2.r);


}
}
Output

Conclusion: we can change values of variablse using constructor.

PRACTICAL 2
Aim: Write a program that defines TriangleArea class with three constructors.
The first form accepts no arguments. The second accept one double
value for radius. The third form accepts any two arguments

import java.util.*;

class TriangleArea{

double b;
int h;

public TriangleArea(){
b=10;
h=23;
System.out.println("area of triangle by default constructor " + b*h/2);
}

public TriangleArea( double b){


h=12;
System.out.println("area of triangle by default constructor " + b*h/2);
}

public TriangleArea( double b,int h){


System.out.println("area of triangle by default constructor " + b*h/2);
}

public static void main(String args[]){


TriangleArea t1= new TriangleArea();
TriangleArea t2= new TriangleArea(23.34);
TriangleArea t3= new TriangleArea(12.23,12);
}
}

Output:

Conclusion: constructor can be over loaded using different parameters or no parameters.

PRACTICAL 3

Aim: Create a class called Employee that includes three pieces of information as instance variables—a
first name (type String), a last name (type String) and a monthly salary (double). Your class should have a
constructor that initializes the three instance variables. Provide a set and a get method for each instance
variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest
that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s
yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.*/

code:
import java.util.*;

class EmployeeTest{

public static void main(String arg[]){


Employee e1=new Employee();
Employee e2=new Employee();

String s2,s3;
double d;

e1.setFname("luther");
e1.setLname("hargrives");
e1.setsal(23000.45);

s2=e1.getFname();
s3=e1.getLname();
d=e1.getsal();

if(d<0.0){
d=0.0;
}

System.out.println( "First name : " + s2 + " last name : " + s3 + " your salary :" + d);
e1.Apresal();
e2.setFname("klaus");
e2.setLname("hargrives");
e2.setsal(-24000.45);

s2=e2.getFname();
s3=e2.getLname();
d=e2.getsal();

if(d<0.0){
d=0.0;
}

System.out.println( "First name : " + s2 + " last name : " + s3 + " your salary : " + d);

class Employee{

String firstname;
String lastname;
double salary;

public Employee(){
firstname="";
lastname="";
salary=0.0;

}
public void setFname(String s1){

this.firstname=s1;
}

public void setLname(String s1){

this.lastname=s1;
}
public void setsal(double s1){
this.salary=s1;
}

public String getFname(){

return this.firstname;

}
public String getLname(){

return this.lastname;

public double getsal(){

return this.salary;

public void Apresal(){

System.out.println(" your salary increased to : " + (this.salary + this.salary*0.10 ) );

Practical 4
/*Create a class called Date that includes three pieces of information as
instance variables—a month (type int), a day (type int) and a year (type
int). Your class should have a constructor that initializes the three
instance variables and assumes that the values provided are correct.
Provide a set and a get method for each instance variable. Provide a
method displayDate that displays the month, day and year separated by
forward slashes (/). Write a test application named DateTest that
demonstrates class Date’s capabilities.*/

class pate{

int year,month,day;

public pate(){
}

public void setY(int y){


this.year=y;
}

public void setM(int m){


this.month=m;

}
public void setD(int d){
this.day=d;
}

public int getY(){


return year;
}

public int getM(){

return month;
}

public int getD(){

return day;
}

public void display()


{
System.out.println("Date : " + getD() + "/" + getM() + "/" + getY());
}

class Date{

public static void main(String arg[] ){

pate d1=new pate();

d1.setY(2004);
d1.setM(11);
d1.setD(12);
d1.display();
}

5
/*Write a program to print the area of a rectangle by creating a class named
'Area' taking the values of its length and breadth as parameters of its
constructor and having a method named 'returnArea' which returns the
area of the rectangle. Length and breadth of rectangle are entered
through keyboard*/

class Area{

static int l,b;

Area(){

Area(int len, int be){

this.l=len;
this.b=be;

public static void returnArea(){

System.out.println("Area of goven rectangle : " + l*b + " " );

public static void main(String arg[]){

Area a1=new Area(34,32);

a1.returnArea();

}
6
/*Print the sum, difference and product of two complex numbers by
creating a class named ‘Complex’ with separate methods for each
operation whose real and imaginary parts are entered by user*/

class complex{

int img,rel;
complex(){}

complex(int r,int i){

this.rel=r;
this.img=i;

}
public complex sum(complex c1,complex c2){

complex c3 = new complex();


c3.rel=c1.rel+c2.rel;
c3.img=c1.img + c2.img;
return c3;
//System.out.ptintln(" Sum of given complex numbers : " + c3.rel + " " + c3.img+ "i" );

}
public complex mul(complex c1,complex c2){

complex c3=new complex();


c3.rel=c1.rel*c2.rel;
c3.img=c1.img * c2.img;
return c3;

//System.out.ptintln(" Sum of given complex numbers : " + c3.rel + " " + c3.img+ "i" );

public complex dif(complex c1,complex c2){

complex c3=new complex();


c3.rel=c1.rel - c2.rel;
c3.img=c1.img - c2.img;

//System.out.ptintln(" Sum of given complex numbers : " + c3.rel + " " + c3.img+ "i" );
return c3;
}

void display()
{

System.out.println(" answer complex numbers : " + rel + " " + img+ "i" );
}
}

class Complex1{
public static void main(String arg[]){
complex c1=new complex(2,3);

complex c2=new complex(4,5);


complex c4= new complex();
complex c3= new complex();
c4=c3.sum(c1,c2);
c4. display();
c4=c3.mul(c1,c2);
c4. display();
c4= c3.dif(c1,c2);
c4. display();
}

Fifth

/*Create a class 'Degree' having a method 'getDegree' that prints "I got a
degree". It has two subclasses namely 'Undergraduate' and 'Postgraduate'
each having a method with the same name that prints "I am an
Undergraduate" and "I am a Postgraduate" respectively. Call the method
by creating an object of each of the three classes*/

class degree{

public degree(){

void getDegree(){

System.out.println("i gor thedefgre");}

public static void main(String args[])


{
degree d=new degree();
undergraduate u=new undergraduate();
postgraduate p =new postgraduate();

d.getDegree();
u.getDegree();
p.getDegree();

}
}

class undergraduate extends degree{

public undergraduate(){

void getDegree(){

System.out.println("i am an undergraduate");

super();
}

}
class postgraduate extends degree{

public postgraduate()
{

void getDegree(){

System.out.println("i am a postgraduate");
super();

Sixth
/*Write a java that implements an interface AdvancedArithmetic which
contains amethod signature int divisor_sum(int n). You need to write a
class calledMyCalculator which implements the interface. divisorSum
function just takes an integer as input and return the sum of all its
divisors.
For example, divisors of 6 are 1, 2, 3 and 6, so divisor_sum should return
12. The value of n will be at most 1000*/
import java.util.*;
interface AdvancedArithmetic{

void divisor_sum(int n);

class six implements AdvancedArithmetic{

public six(){

}
public void divisor_sum(int n){

int c=0;
for(int i=1;i<=n;i++){
if(n%i==0){

c=c+i;
}

}
System.out.println(" your answer" + c);
}

public static void main (String args[]){

six c1=new six();

Scanner sc=new Scanner(System.in);


int k=sc.nextInt();

c1.divisor_sum(k);

}
Seventh

/*Assume you want to capture shapes, which can be either circles (with a
radiusand a color) or rectangles (with a length, width, and color). You
also want to be able to create signs (to post in the campus center, for
example), each of which has a shape (for the background of the sign) and
the text (a String) to put on the sign. Create classes and interfaces for
circles, rectangles, shapes, and signs. Write a program that illustrates the
significance of interface default method.
*/

interface shape{

default void color(){


System.out.println("color is black");

}
}

interface sign extends shape{

void sign1();

class rectangle implements sign{

public rectangle(){
System.out.println(" 90 as rectangle");

public void sign1(){

System.out.println("upside and downside cosidered as rectangle");


}

}
class circle1 implements sign{

public circle1(){
System.out.println("45 is area circle");

}
public void sign1(){

System.out.println("right side and left side cosidered as circle");


}

public static void main(String args[]){


circle1 c1=new circle1();
rectangle r1=new rectangle();
c1.sign1();
c1.color();
r1.color();
r1.sign1();

You might also like