Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Thursday, 22 September 2011

Java program to calculate total cost of an item using methods (OOP)

import java.util.*;
class invoice
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
String name;
itemdata ob = new itemdata();
ob.get();
System.out.print ("\n\t Enter item name = ");
name=in.nextLine();
ob.cost();
ob.displyLine(name);

}
}


class itemdata
{
Scanner in = new Scanner (System.in);
int number;
double quantity,price,tcost=0;
void get()
{
System.out.print ("\n\t Enter item number = ");
number=in.nextInt();
}

void cost()
{
System.out.print ("\n\t Enter item quantity = ");
quantity=in.nextDouble();
System.out.print ("\n\t Enter item price per unit = ");
price=in.nextDouble();
tcost=quantity*price;
}

void displyLine(String name)
{
System.out.println ("\n\t Item number = "+number);
System.out.println ("\n\t Item nume = "+name);
System.out.println ("\n\t Item quantity = "+quantity);
System.out.println ("\n\t Item price per unit = "+price);
System.out.println ("\n\t Total cost = "+tcost);
}

}

Wednesday, 21 September 2011

Java program to calculate salary of an employe using methods

import java.util.*;
class salary
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double hpr,rh,oh;
System.out.print ("\n\t Enter hourly pay rate = ");
hpr=in.nextDouble();
System.out.print ("\n\t Enter regular hours = ");
rh=in.nextDouble();
System.out.print ("\n\t Enter overtime hours = ");
oh=in.nextDouble();

double rhp=0,otp,tp=0;
rhp = hpr*rh;                            //calculating regular hours pay: rhp

calculate ob = new calculate();            //creating object of class calculate
otp=ob.overtimepay(hpr,oh);                   //calling the method overtimepay
tp=rhp+otp;                                //calculating total pay

System.out.println ("\n\t Regular hours pay = "+rhp);
System.out.println ("\n\t Overtime pay = "+otp);
System.out.println ("\n\t Total pay = "+tp);
}
}


class calculate
{
double otp;
double overtimepay (double hpr, double oh)        //recieving hourly pay rate and overtime hours hpr, oh respectively
{
otp=(hpr*0.5)*oh;                                //calculating overtime pay
return otp;
}
}