You are on page 1of 14

Program 1: import java.util.Scanner; class triangle { static Scanner skd = new Scanner(System.

in); double base, height; void getdata() { System.out.println("For a right angled triangle :: \nEnter the base ::"); base=skd.nextDouble(); System.out.println("Enter the height::"); height=skd.nextDouble(); }

void display() { double hypo = Math.sqrt((base*base)+(height*height)); System.out.println("Perimeter :: " +(base + height + hypo)); System.out.println("Arear :: " +(0.5*base*height)); }

public static void main(String args[]) { triangle obj = new triangle(); obj.getdata(); obj.display(); } }

Output For a right angled triangle ::

Enter the base :: 21.3 Enter the height:: 24.6 Perimeter :: 78.43997541486472 Arear :: 261.99

Program 3 import java.io.*; class income { BufferedReader skd = new BufferedReader(new InputStreamReader(System.in)); double basic_sal, tot_sal, gross_sal, tax; char staff_cntn;

void getdata()throws IOException { System.out.println("Enter employee details ::\nBasic Salary ::"); basic_sal = Double.parseDouble(skd.readLine()); System.out.println("Staff canteen(y/n) ::"); staff_cntn = skd.readLine().charAt(0); }

void paysal() { System.out.println("Basic salary = "+basic_sal); System.out.println("Allowances ::\nDearness Allowance = "+(basic_sal*0.8)); System.out.println("House Rent allowance = "+(basic_sal*0.15)); gross_sal = (basic_sal*(1+0.8+0.15)); System.out.println("Gross Salary = "+gross_sal); deduction();

System.out.println("Net Salary to be paid = "+tot_sal); calc_tax(); }

void deduction() { double deduc = 0.0; System.out.println("Deductions in terms of basic pay ::"); if(staff_cntn=='y'); { deduc = deduc + 0.8; System.out.println("Staff canteen (8%) :" +(basic_sal*0.8)); } System.out.println("net deduction ="+basic_sal*deduc); tot_sal = gross_sal - (basic_sal*deduc); }

void calc_tax() { double ann_sal = gross_sal*12; System.out.println("Annual salary ="+ann_sal); System.out.println("Income tax ="); if(ann_sal < 100000) { tax = ann_sal*0.2; System.out.println(tax+"20%"); } else { tax = ann_sal*0.3; System.out.print(tax+"(30%)");

System.out.println("Tax surcharge(10% of I.T.)="+tax*0.1); } }

public static void main(String args[])throws IOException { income i = new income(); i.getdata(); i.paysal(); } }

Output Enter employee details :: Basic Salary :: 5800.67 Staff canteen(y/n) :: y Basic salary = 5800.67 Allowances :: Dearness Allowance = 4640.536 House Rent allowance = 870.1005 Gross Salary = 11311.3065 Deductions in terms of basic pay :: Staff canteen (8%) :4640.536 net deduction =4640.536 Net Salary to be paid = 6670.7705000000005 Annual salary =135735.678 Income tax = 40720.703400000006(30%)Tax surcharge(10% of I.T.)=4072.0703400000007

Program 4 import java.io.*; class employee { BufferedReader skd = new BufferedReader(new InputStreamReader(System.in)); long empno; int dd, mm, yy; char rank; double salary; employee() { empno = 0; rank = '0'; dd = mm = yy = 0; salary = 0.0; }

void getdata()throws IOException { System.out.println("Enter the employee details\nEmployee Number :"); empno = Long.parseLong(skd.readLine()); System.out.println("Date of Birth ::"); System.out.println("dd= "); dd = Integer.parseInt(skd.readLine()); System.out.println("mm= "); mm = Integer.parseInt(skd.readLine()); System.out.println("yy= "); yy = Integer.parseInt(skd.readLine()); System.out.println("Rank (A-D) :"); rank = skd.readLine().charAt(0); System.out.println("Salary :Rs. ");

salary = Double.parseDouble(skd.readLine()); }

void display() { System.out.println("Employee details::\nEmployee number :"+empno); System.out.println("Date of birth :"+dd+"-"+mm+"-"+yy); System.out.println("Rank : Group"); switch(rank) { case 'a':System.out.println("a"); break; case 'b':System.out.println("b"); break; case 'c':System.out.println("c"); break; case 'd':System.out.println("d"); break; } }

void upgrade() { salary =salary * 1.10; if(rank > 'a') rank = 1; }

public static void main(String []args)throws IOException { employee e = new employee(); System.out.println("before entry of employee::::"); e.display(); e.getdata(); System.out.println("After entry::::");

e.display(); System.out.println("after upgradation::::"); e.upgrade(); e.display(); System.out.println("10% increment in salary"); } }

Output before entry of employee:::: Employee details:: Employee number :0 Date of birth :0-0-0 Rank : Group Enter the employee details Employee Number : 896 Date of Birth :: dd= 31 mm= 7 yy= 1987 Rank (A-D) : c Salary :Rs. 32000 After entry:::: Employee details:: Employee number :896

Date of birth :31-7-1987 Rank : Group c after upgradation:::: Employee details:: Employee number :896 Date of birth :31-7-1987 Rank : Group 10% increment in salary

Program 5

import java.util.Scanner; class student { String rollno; String dept; int year, sem; student() { rollno = "DTU/2k12/..."; dept = "Not Allotted"; year = 0; sem = 0; } student(String rno, String deptt, int year, int sem) { rollno = rno; dept = deptt; this.year = year; this.sem = sem; } void display() { System.out.println("Student details::\nDepartment ::"+dept); System.out.println("Year::"+year);

System.out.println("Semester ::"+sem); System.out.println("Roll no. ::"+rollno); } public static void main(String [] args) { Scanner skd = new Scanner(System.in); student s = new student(); System.out.println("before registration:::"); s.display(); System.out.println("update registration details:::\nEnter the Department :"); String dept = skd.nextLine(); System.out.println("Enter roll no. :"); String rollno = skd.nextLine(); System.out.println("Enter year :"); int year = skd.nextInt(); System.out.println("Enter semester :"); int sem = skd.nextInt(); student s2 = new student(rollno, dept, year, sem); s2.display(); } }

output program 5 : before registration::: Student details:: Department ::Not Allotted Year::0 Semester ::0 Roll no. ::DTU/2k12/... update registration details::: Enter the Department : se Enter roll no. : 75 Enter year : 2 Enter semester : 3 Student details:: Department ::se Year::2 Semester ::3 Roll no. ::75

class Node { public int info; public Node next; }

import java.util.Scanner; public class list { static Scanner br = new Scanner(System.in); //for input of values in list Node start; //head node list() { start = null; //initialize head to null b default constructor } public void create() //constructing a new list { Node ptr = null; //new null ptr System.out.println("Enter number of elements in the list"); int n = br.nextInt(); System.out.println("Enter elements in the list"); for(inti=1; i<=n; i++) //loop to input 5 elements initially { if(i == 1) start = ptr = new Node(); else ptr = ptr.next; ptr.info = br.nextInt(); if(i == n) ptr.next = null; else ptr.next = new Node(); } } void display() { Node ptr = start; while(ptr!=null) { System.out.println(ptr.info +" "); ptr = ptr.next; } System.out.println(); } void insert(intval, intpos) { Node ptr = null; if(pos == 1) { Node temp = new Node(); temp.info = val; temp.next = start; start = temp; } int c = 0; for(ptr = start; ptr!=null; ptr = ptr.next) { c++; if(c == pos - 1) { Node temp = new Node(); temp.info = val; temp.next = ptr.next; ptr.next = temp; } } }

void del(intpos) { Node ptr = null; int c = 0; if(pos == 1) { start = start.next; } for(ptr = start; ptr!=null; ptr = ptr.next) { c++; if(c == pos-1) ptr.next = ptr.next.next; } } public static void main(String args[]) { int choice, v, pos; listobj = new list(); loop 1: for(;;) { System.out.println("Enter 1 to create\n2 to insert\n3 to delete\n4 to display a list\nEnter 5 to end"); choice = br.nextInt(); switch(choice) { case 1: obj.create(); break; case 2: System.out.println("enter the value to be inserted:"); v = br.nextInt(); System.out.println("enter the position at which to be inserted:"); pos = br.nextInt(); obj.insert(v, pos); break; case 3: System.out.println("enter the position to be deleted:"); pos = br.nextInt(); obj.del(pos); break; case 4: obj.display(); break; case 5: break loop1; default:System.out.println("Invalid choice"); } } } program 6 updated output:: Enter 1 to create 2 to insert 3 to delete 4 to display a list Enter 5 to end 1 Enter number of elements in the list 3 Enter elements in the list 23 98 14 Enter 1 to create 2 to insert 3 to delete 4 to display a list Enter 5 to end 2

enter the value to be inserted: 94 enter the position at which to be inserted: 1 Enter 1 to create 2 to insert 3 to delete 4 to display a list Enter 5 to end 3 enter the position to be deleted: 3 Enter 1 to create 2 to insert 3 to delete 4 to display a list Enter 5 to end 4 94 23 14 Enter 1 to create 2 to insert 3 to delete 4 to display a list Enter 5 to end 5

Program 9 class polymorpharea { double length; double breadth; double radius; double pi;

polymorpharea(double x, double y, double z, double p) { length = x; breadth = y; radius = z; pi = p; }

double area()

{ return(length*breadth*radius*pi); }

public static void main(String args[]) { polymorpharea obj1 = new polymorpharea(0.0,0.0,0.0,0.0); double ar1 = obj1.area(); System.out.println("area of point = "+ar1); polymorpharea obj2 = new polymorpharea(20.45,0.0,0.0,0.0); double ar2 = obj2.area(); System.out.println("area of line = "+ar2); polymorpharea obj3 = new polymorpharea(45.98,23.65,1.0,1.0); double ar3 = obj3.area(); System.out.println("area of rectangle = "+ar3); polymorpharea obj4 = new polymorpharea(76.9,76.9,1.0,1.0); double ar4 = obj4.area(); System.out.println("area of square = "+ar4); polymorpharea obj5 = new polymorpharea(1.0,1.0,23.13,3.14); double ar5 = obj5.area(); System.out.println("area of circle = "+ar5); } }

Output area of point = 0.0 area of line = 0.0 area of rectangle = 1087.427 area of square = 5913.610000000001

area of circle = 72.6282

You might also like