Input name of a customer and purchase amount. Find discount and net amount.
Discount rate is 10% of the purchase amount. Print all details.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name and purchase amount");
String na=sc.nextLine();
double amt=sc.nextDouble();
double dis=10/100.0*amt;
double net=amt-dis;
System.out.println("Name: "+na);
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2. Conditional Construct: Selection Statement
2.1. if
Input purchase amount. Find discount and net amount using simple if.
If amount is 5000 or above then discount rate is 10% of the amount (otherwise no need to calculate discount).
Print purchase amount, discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis=0.0;
if(amt>=5000)
dis=10/100.0*amt;
double net=amt-dis;
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.2. if else
Input purchase amount. Find discount and net amount.
If amount is 5000 or above then discount rate is 10% of the amount, otherwise 8%.
Print purchase amount, discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis;
if(amt>=5000)
dis=10/100.0*amt;
else
dis=8/100.0*amt;
double net=amt-dis;
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.3. if else if else
Input purchase amount. Find discount and net amount. Discount rate is as follows:
If amount is 5000 or above then 10% of the amount
If it is above 3000 then 8%
Otherwise 5% Print discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis;
if(amt>=5000)
dis=10/100.0*amt;
else if(amt>3000)
dis=8/100.0*amt;
else
dis=5/100.0*amt;
double net=amt-dis;
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.4. Nested if else if
Input purchase amount and category. Find discount and net amount; print details. The rate is given below:
Amount Gents Dress Ladies Dress
5000 or above 10% 12%
3001 to 4999 8% 10%
3000 and below 5% 8%
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount and category [G/L]");
double amt=sc.nextDouble();
char category=sc.next().charAt(0);
double dis;
if(category==‘G’ || category==‘g’)
{
if(amt>=5000)
dis=10/100.0*amt;
else if(amt>3000)
dis=8/100.0*amt;
else
dis=5/100.0*amt;
}
else if(category==‘L’ || category==‘l’)
{
if(amt>=5000)
dis=12/100.0*amt;
else if(amt>3000)
dis=10/100.0*amt;
else
dis=8/100.0*amt;
}
else
{
System.out.println("No discount");
dis=0.0;
}
double net=amt-dis;
System.out.println("Purchase Amount: "+ amt);
System.out.println("Category: "+ category);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.5. switch case
Write a menu-driven program: 1) Calculate discount and net amount. Discount rate is 10% on the amount
2) Calculate tax and total. Tax is 12% on the amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 Discount");
System.out.println("2 Tax");
System.out.println("Enter your choice");
int ch=sc.nextInt();
double amt,dis,net,tax,tot;
switch(ch)
{
case 1: System.out.println("Enter purchase amount");
amt=sc.nextDouble();
dis=10/100.0*amt;
net=amt-dis;
System.out.println("Discount: "+dis);
System.out.println("Net amount: "+net);
break;
case 2: System.out.println("Enter purchase amount");
amt=sc.nextDouble();
tax=12/100.0*amt;
tot=amt+tax;
System.out.println("Tax: "+tax);
System.out.println("Total: "+tot);
break;
default: System.out.println("Wrong input");
)
}
}
3. Iterative Construct (Loop):
3.1. for
Input name and purchase amount of n customers. If amount is 5000 or above then discount
rate is 10% of the amount, if it is above 3000 then 8% otherwise 5%. Print bill for each customer. Also find and print total net amount collected before closing the shop. Bill Format is given below:
Sl.No.NameAmountDiscountNet Amount
1xxxxxxxxxxxx
Total net amount collected: xxx
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of customers");
int n=sc.nextInt();
double tot=0.0;
for(int i=1;i<=n;i++)
{
System.out.println("Enter name and purchase amount");
Input name and purchase amount of some customers using while loop. If amount is 5000 or above then discount
rate is 10% of the amount, if it is above 3000 then 8% otherwise 5%. Print bill for each customer. Also find and print total net amount collected before closing the shop. Bill Format is given below:
Sl.No.NameAmountDiscountNet Amount
1xxxxxxxxxxxx
Total net amount collected: xxx The program should continue as long as user wishes.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
char ch='y'; int i=1;
double tot=0.0;
while(ch=='Y'||ch=='y')
{
System.out.println("Enter name and purchase amount");
Input name and purchase amount of some customers using do while loop. If amount is 5000 or above then discount
rate is 10% of the amount, if it is above 3000 then 8% otherwise 5%. Print bill for each customer. Also find and print total net amount collected before closing the shop. Bill Format is given below:
Sl.No.NameAmountDiscountNet Amount
1xxxxxxxxxxxx
Total net amount collected: xxx The program should continue as long as user wishes.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double tot=0.0; int i=1;
do
{
System.out.println("Enter name and purchase amount");
Input name and purchase amount of n customers. If amount is 5000 or above then discount rate is 10%
of the amount, if it is above 3000 then 8% otherwise 5%. Print a list of purchase with seriel number. Also find total net amount
collected in the shop. Format: List of Purchase
Sl. No.NameAmountDiscountNet Amount
1------------
2------------
Total net amount collected: ---
import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of customers");
int n=sc.nextInt();
String name[]=new String[n];
double amt[]=new double[n];
double dis[]=new double[n];
double net[]=new double[n];
double tot=0.0;
for(int i=0;i<n;i++)
{
System.out.println("Enter name and purchase amount");
System.out.println("Total net amount collected: "+ tot);
}
}
4.2. D D Array
Input 50 customers’ 7 days’ purchase amounts. Find each customer’s total purchase amounts
and each day's total amounts.
import java.util.*;
public class Marks
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double amt[][]=new double[50][7];
int i,j;
//To input purchase amounts
for(i=0;i<50;i++)
{
for(j=0;j<7;j++)
{
System.out.println("Enter purchase amount: ");
amt[i][j]=sc.nextDouble();
}
}
//To find total amount of each customer
double s;
for(i=0;i<50;i++)
{
s=0.0;
for(j=0;j<7;j++)
{
s=s+amt[i][j];
}
System.out.println("Total amount of customer"+(i+1)+" : "+s);
}
//To find total amount of each customer
for(i=0;i<7;i++)
{
s=0.0;
for(j=0;j<50;j++)
{
s+=amt[j][i];
}
System.out.println("Total amount of day"+(i+1)+" : "+s);
}
}
}
5. Methods
5.1. Overloaded Methods
W a p using overloaded methods void amount() to find followings:
1) Find discount and net amount. Discount rate is 10% of purchase amount
2) Find tax and total amount. Tax rate is 15% of sales amount
import java.util.*;
public class Numbers
{
public static void amount(double amt)
{
double dis=amt*10/100.0;
double net=amt-dis;
System.out.println("Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net amount: "+net);
}
public static void amount(float amt)
{
float tax=amt*15/100.0;
double tot=amt+tax;
System.out.println("Amount: "+amt);
System.out.println("Tax: "+tax);
System.out.println("Total: "+tot);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double pamt=sc.nextDouble();
amount(pamt);
System.out.println("Enter sales amount");
float samt=sc.nexFloat();
amount(samt);
}
}
5.2. Overloaded Methods with Menu-driven
Write a menu-driven program using overloaded methods void amount() to find followings:
1) Find discount and net amount. Discount rate is 10% of purchase amount
2) Find tax and total amount. Tax rate is 15% of sales amount
import java.util.*;
public class Numbers
{
public static void amount(double amt)
{
double dis=amt*10/100.0;
double net=amt-dis;
System.out.println("Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net amount: "+net);
}
public static void amount(float amt)
{
float tax=amt*15/100.0;
double tot=amt+tax;
System.out.println("Amount: "+amt);
System.out.println("Tax: "+tax);
System.out.println("Total: "+tot);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\t\t\tMenu");
System.out.println("1 Discount");
System.out.println("2 Tax");
System.out.print("Enter your choice: ");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter purchase amount");
double amt1=sc.nextDouble();
amount(amt1);
break;
case 2: System.out.println("Enter sales amoun");
float amt2=sc.float();
amount(amt2);
break;
default: System.out.println("Wrong entry");
}
}
}
6.1. Constructor
Define a class Discount. Data members / Instance variables: name, amount, discount and net.
Member methods: 1) input() - to input name and purchase amount.
2) calculate() – to find discount and net amount:
If amount is 5000 or above then discount rate is 10% of the amount,
if it is above 3000 then 8% otherwise 5%.
3) display() – to display values. Write main() also.
import java.util.*;
public class Discount
{
String name;
double amount;
double discount,net;
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name and purchase amount");