Input name of customer, consumer number, previous reading and present reading.
Find number of units used, electricity charge and print a bill. The charge is Rs. 2.50 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, consumer number, previous and present reading");
Input previous reading and present reading. Find the units used, electricity charge and print a bill.
If the units is above 40 then the charge is Rs. 2.50 per unit. (For others no charge). Use simple if.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input previous reading and present reading. Find the electricity charge and print the bill.
For the first 40 units the charge is Rs. 2.50 per unit. For remaining units Rs. 4.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input previous reading and present reading. Find electricity charge and print a bill.
For the first 40 units the charge is Rs. 2.50 per unit. Next 40 units Rs. 4.00 per unit.
For remaining units Rs. 6.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
Input number of units and category. Find electricity charge. The calculation is given below:
Number of units Domestic (D) (Rs. per unit) Business (B) (Rs. per unit)
For first 40 units 2.50 3.50
Next 40 units 4.00 5.00
Remaining units 6.00 7.00
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of units and category");
int units=sc.nextInt();
char c=sc.next().charAt(0);
double charge;
if(c==’D’||c==’d’)
{
if(units<=40)
charge= units*2.5;
else if(units<=80)
charge=40*2.5+(units-40)*4.0;
else
charge= 40*2.5+40*4.0+(units-80)*6.0;
}
else if(c==’B’||c==’b’)
{
if(units<=40)
charge= units*3.5;
else if(units<=80)
charge=40*3.5+(units-40)*5.0;
else
charge= 40*3.5+40*5.0+(units-80)*7.0;
}
else
{
System.out.println("Wrong input");
charge=0.0;
}
System.out.println("Charge: "+ charge);
}
}
2.5. if After if else
Input previous reading and present reading. Find electricity charge and print a bill.
For the first 40 units charge is Rs. 2.50 per unit.
For remaining units Rs. 4.00 per unit.
For Electricity Board employees no charge.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter previous and present reading");
W a m d p: Input category as choice and units. Find electricity charge. Calculation is given below:
Number of units Domestic (D) (Rs. per unit) Business (B) (Rs. per unit)
For first 40 units 2.50 3.50
Next 40 units 4.00 5.00
Remaining units 6.00 7.00
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("D Domestic");
System.out.println("B Business");
System.out.println("Enter your choice");
char ch=sc.next().charAt(0);
double charge;
switch(ch)
{
case ‘D’ : System.out.println("Enter units");
int units=sc.nextInt();
if(units<=40)
charge= units*2.5;
else if(units<=80)
charge=40*2.5+(units-40)*4.0;
else
charge=40*2.5+40*4.0+(units-80)*6.0;
System.out.println("Charge: "+ charge);
break;
case ‘B’ : System.out.println("Enter units");
units=sc.nextInt();
if(units<=40)
charge=units*3.5;
else if(units<=80)
charge=40*3.5+(units-40)*5.0;
else
charge= 40*3.5+40*5.0+(units-80)*7.0;
System.out.println("Charge: "+ charge);
break;
default: System.out.println("Wrong choice");
)
}
}
3. Iterative Construct (Loop):
3.1. for
Input n subscribers’ names and number of units. Find electricity charge and print a bill
for each subscriber. Also find total collection. For first 40 units charge is Rs. 2.50 per unit.
Remaining units Rs. 4.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of subscribers: ");
int n=sc.nextInt();
double tot=0.0;
for(int i=1;i<=n;i++)
{
System.out.println("Enter name and number of units");
String name= sc.nextLine();
int units=sc.nextInt();
double charge;
if(units<=40)
charge= units*2.5;
else
charge= 40*2.5+(units-40)*4.0;
tot+=charge;
System.out.println("Name: "+name);
System.out.println("Units: "+units);
System.out.println("Charge: "+charge);
}
System.out.println("Total collection: "+tot);
}
}
3.2. do while
Input names of some subscribers and their number of units. Find electricity charge and print a bill
for each subscriber. Also find total collection.
For the first 40 units the charge is Rs. 2.50 per unit.
Remaining units Rs. 4.00 per unit. The program continues as long as user wishes.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double tot=0.0;
char ch;
do
{
System.out.println("Enter name and units");
String name= sc.nextLine();
int units =sc.nextInt();
double charge;
if(units<=40)
charge= units*2.5;
else
charge= 40*2.5+(units-40)*4.0;
tot+=charge;
System.out.println("Name: "+name);
System.out.println("Units: "+units);
System.out.println("Charge: "+charge);
System.out.println("Do you want to continue? [Y/N]: ");
ch=sc.next().charAt(0);
}
while(ch==‘Y’||ch==‘y’);
System.out.println("Total collection: "+tot);
}
}
4.1. S D Array:
Input n subscribers’ consumer numbers, names and number of units into three arrays. Find electricity charge
of each subscriber and store them into another array. Input a search number; print the details of the subscriber.
Nnumbers are not sorted.
For first 40 units charge is Rs. 2.50 per unit.
Remaining units Rs. 4.00 per unit.
import java.util.*;
public class Bill
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of subscribers: ");
int n=sc.nextInt();
int cn[] = new int[n];
String name[] = new String[n];
int unit[] = new int[n];
double charge[] = new double[n];
int i;
for(i=0;i<n;i++)
{
System.out.println("Enter number, name and number of units");
Initialize integers to a 4 x 4 matrix. Display the left and right diagonals simultaneously and find average of them together.
public class Matrix
{
public static void main()
{
int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
//To display the diagonals and find sum of them
System.out.println("The diagonals");
int i,j,c=0;
double s=0.0,avg;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j||i+j==3)
{
System.out.print(a[i][j]+"\t");
s=s+a[i][j];
c++;
}
else
System.out.print("\t");
}
System.out.println();
}
avg=s/c;
System.out.println("Average of the diagonals : "+avg);
}
}
5.1. Methods
Overloaded Methods
W a p using overloaded methods void charge() to find followings:
1) Find electricity charge for Domestic use First 40 units: 2.50 per unit. Next 40 units: 4.00 per unit: Remaining: 6.00 per unit
2) Find electricity charge for Business use First 40 units: 4.50 per unit. Next 40 units: 6.00 per unit: Remaining: 8.00 per unit
import java.util.*;
public class Bill
{
static Scanner sc=new Scanner(System.in);
public static void charge(long unit)
{
double charge;
if(units<=40)
charge= units*2.5;
else if(units<=80)
charge= 40*2.5+(units-40)*4.0;
else
charge= 40*2.5+40*4.0+(units-80)*6.0;
System.out.println("Charge: "+charge);
}
public static void charge(int unit)
{
double charge;
if(units<=40)
charge= units*4.5;
else if(units<=80)
charge= 40*4.5+(units-40)*6.0;
else
charge= 40*4.5+40*4.0+(units-80)*8.0;
System.out.println("Charge: "+charge);
}
public static void main()
{
System.out.println("Enter units of Domestic use");
long u1=sc.nextLong();
charge(u1);
System.out.println("Enter units of Business use");
int u2=sc.nextInt();
charge(u2);
}
}
6.1. Constructor
Define a class ElectricityBill. Data members / Instance variables: name, unit, charge.
Member methods: 1) input() - to input name and units.
2) compute() – to find elecricity charge:
First 40 units: 4.50 per unit. Next 40 units: 6.00 per unit: Remaining: 8.00 per unit
3) print() – to print the bill. Write main() also.