Programs using custom classes,constructors

   program with custom class (name, salary, new name)

package com.company;
import java. util.Scanner;
class employee {
int salary;
String name;
public void su() {
System.out.println("salary is" + salary);
}
public void nm() {
System.out.println("name is"+name);
}
public void setn(){
System.out.println("new name is "+ name);
}
}
public class custom_class {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
employee e = new employee();
e.salary = 10000;
e.name = "vishal rana";

e.su();
e. nm();

System.out.println("enter the new name");
String n= s.nextLine();
e.name=n;
e.setn();
}
}

program to find the area, parameter of the square using class.

package com.company;
class square{
int side;
public void sid()
{
System.out.println("side of square is "+side);
}
public void parameter()
{
System.out.println("parameter of the square is "+ 4*side);
}
public void area(){
System.out.println("area of the square is "+ side*side);
}
}
public class class_square {
public static void main(String[] args) {
square s=new square();
s.side =8;
s.sid();
s.parameter();
s.area();
}
}

program to find the area, parameter of the rectangle using class.

package com.company;
class rectangle{
int len;
int wid;
public void sid()
{
System.out.println("length of rectangle is "+len);
System.out.println("width of rectangle is "+wid);
}
public void parameter()
{
System.out.println("parameter of the rectangle is "+ 2*(len+wid));
}
public void area(){
System.out.println("area of the rectangle is "+ len*wid);
}
}
public class class_square {
public static void main(String[] args) {
rectangle r=new rectangle();
r.len =8;
r.wid=4;
r.sid();
r.parameter();
r.area();
}
}

program to add two numbers using constructors 

package com.company;
class cls{
private int a=0,b=0;
public void asign(){
a=5;
b=3;
System.out.println(a+b);
}
cls(){
System.out.println("sum of the two numbers is: ");
}
}
public class constructors {
public static void main(String[] args) {
cls c= new cls();
c.asign();
}
}



No comments:

Post a Comment

    Hello, I'm Vishal Rana  I'm 19 years old  I live in Himachal Pradesh and  currently I'm studying in college for my UG degree...