Showing posts with label Programs using else if. Show all posts
Showing posts with label Programs using else if. Show all posts

Sunday, August 18, 2019

BlueJ Program on Fine For Late Return of Book

This is a BlueJ Program on Library late fine calculation.


Write a program to enter number of days late to return a book and display fine amount as per chart.

Number of days
Fine per day (Rs)
First 5
1.50
Nest 7
2.50
Nest 15
5.00
Above 27
10,00





import java.util.*;
public class LateFine
{
    Scanner sc=new Scanner(System.in);
    int d;
    double fine;
   
    public void takeDatas()
    {           
            System.out.print("\nEnter Number of Days Late in Returning Book:");
            d=sc.nextInt();
            if(d<=5)
            {
                 fine=d*1.50;
                 d=0;
                }
                else
                {
                     fine=5*1.50;
                     d=d-5;
                    }
                 if(d<=7 && d >0)
            {
                 fine=fine+d*2.50;
                 d=0;
                }
                else if(d >0)
                {
                     fine=7*2.50;
                     d=d-7;
                    }
                    if(d<=15 && d >0)
            {
                 fine=fine+d*5.00;
                 d=0;
                }
                else if(d >0)
                {
                     fine=15*5.00;
                     d=d-15;
                    }
                    if(d >0)
                    {
                        fine=d*15.00;
                    }
          
        System.out.println("\nTotal Fine:"+fine);
       
    }
           
                  
            public static void main(String args[])
            {
                 LateFine obj=new LateFine();
                 obj.takeDatas();
             }
            }

Friday, December 3, 2010

BlueJ programs on electric bill and customer bill


It is observed from different ICSE Computer Application papers of previous years  that one program on else if ladder can be expected in ICSE 2011 Computer examination. Two types of such program are given below. 
The first program is a simple one.

Details of the program

A customer purchase items from a shop and if the purchase amount exceeds or equals to Rs. 5000, then the discount is applicable.
The slabs of discount are as follows:
 Purchase amount >=5000 and <7000 – Discount is 2%  on purchase
Purchase amount >=7000 and <10000 – Discount is 2.5%  on purchase
Purchase amount above 10000 – Discount is 2.75%  on purchase

Here in this program we have to take the amount of purchase from user and the payable amount to be displayed.

About the program

We will simply check the block where the purchase amount fits and the discount amount will be deducted from the purchased amount.

 import java.io.*;
class bill
{
double payable ,purchase;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeAmount()throws IOException
{
System.out.println("Enter the purchased amount");
purchase=Double.parseDouble(br.readLine().trim());
}
public void dispBill()
{
if(purchase>=5000)
{
if(purchase< =7000)
{
payable=purchase-purchase*2/100;
}
else if(purchase< =10000)
{
payable=purchase-purchase*2.5/100;
}
else
{
payable=purchase-purchase*2.75/100;
}
}
else
payable=purchase;
System.out.println("payable amount="+payable);
}
public static void main(String args[])throws IOException
{
bill ob=new bill();
ob.takeAmount();
ob.dispBill();
}
}

The second BlueJ program is little bit difficult. This program is on calculating electric bill of a customer.

Details of the program

Monthly rent of electricity is Rs.200.00 and 50 units per month is free.
On remaining units consumed first 100units are charged at the rate of Rs. 1 per unit
On remaining units, the first 100units are charged at the rate of Rs. 1.5 per unit
The rest units are charged at the rate of Rs. 2 per unit

Here in this program we have to take the units consumed from user and the payable amount to be displayed.

About the program

This program needs lots of care. Firstly the rent is fixed and a customer has to pay it whatever may be the units consumed. If the units exceed 50 units then only calculations are required and in such case 50 to be deducted from total units. (50 units is free). For first 100 units or the remaining units –  which is less, is multiplied by the rate per unit. If the units are less than 100 then ‘unit’ variable is reinitialized with -1 so that the last ‘if(unit>0)’ statement is not encountered. This process is carried out in all the if and else.

 import java.io.*;
class Electric
{
double payable=200;
int unit;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeAmount()throws IOException
{
System.out.println("Enter the Units consumed");
unit=Integer.parseInt(br.readLine().trim());
}
public void dispBill()
{
if(unit>50)
{
unit=unit-50;
if(unit>100)
{
payable=payable+100*1;
unit=unit-100;
}
else
{
payable=payable+unit*1;
unit=-1;
}
if(unit>100)
{
payable=payable+100*1.5;
unit=unit-100;
}
else
{
payable=payable+unit*1.5;
unit=-1;
}
 if(unit>0)
{
payable=payable+unit*2;
}
}
System.out.println("payable amount="+payable);
}
public static void main(String args[])throws IOException
{
Electric ob=new Electric();
ob.takeAmount();
ob.dispBill();
}
}


In case of both the programs if you are using BlueJ editor to execute these programs, remove the function ‘public static void main(String args[])’ from the coding.

Thursday, November 25, 2010

BlueJ Program on displaying day name from the entered date in specific format

Here is the BlueJ Program for ISC Students.

Write a program to accept a date in a specific string format (dd/mm/yyyy) and accept the name of the day on 1st January of the corresponding year. Find the day for the given date.

Example:
Input:
Enter the date ( in dd/mm/yyyy) format: 5/7/2001
Enter the Day on 1st January in this year: Monday

Output:
5/7/2001 : Thursday

import java.io.*;
class Date1
{
int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
BufferedReader br;
String str1,day,day1;
int x,i,dayno,mon,yr,leap1;
public static void main(String args[])throws IOException
{
Date1 ob=new Date1 ();
ob.take();
ob.show();
}
Date1 ()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public void take()throws IOException
{
System.out.println("Enter the date ( in dd/mm/yyyy) format:");
day=br.readLine().trim();
day1=day;
i=day.indexOf("/");
dayno=Integer.parseInt(day.substring(0,i));
day=day.substring(i+1);
i=day.indexOf("/");
mon=Integer.parseInt(day.substring(0,i));
day=day.substring(i+1);
yr=Integer.parseInt(day);
leap1=0;
if(mon>2)
leap1=leap(yr);
System.out.println("Enter the Day on 1st January in this year:");
str1=br.readLine().trim();
}
int leap(int p)
{
if(p%100==0 && p%400==0)
return 1;
else if(p%100!=0 && p%4==0)
return 1;
else
return 0;
}
void show ()
{
if (str1.equalsIgnoreCase("Sunday"))
x=1;
else if (str1.equalsIgnoreCase("Monday"))
x=2;
else if (str1.equalsIgnoreCase("Tuesday"))
x=3;
else if (str1.equalsIgnoreCase("Wednesday"))
x=4;
else if (str1.equalsIgnoreCase("Thursday"))
x=5;
else if (str1.equalsIgnoreCase("Friday"))
x=6;
else if (str1.equalsIgnoreCase("Saturday"))
x=7;

for(i=0;i< mon-1;i++)
dayno=dayno+arr[i];
dayno=dayno+leap1;
for(i=0;i< dayno-1;i++)
{
 x++;
 if(x==8)
 x=1;
 }
 System.out.println(day1+ ":");
switch(x)
{
 case 1:
 System.out.println("Sunday");
 break;
 case 2:
 System.out.println("Monday");
 break;
 case 3:
 System.out.println("Tuesday");
 break;
 case 4:
 System.out.println("Wednesday");
 break;
 case 5:
 System.out.println("Thursday");
 break;
 case 6:
 System.out.println("Friday");
 break;
 case 7:
 System.out.println("Saturday");
 break;
}
 }
 }


 Here in this BlueJ program the array ‘arr’ holds the number of days of each month. ‘take()’ function takes the date and day name of 1st January of the specific year. It also breaks the date into day, month and year. If the month is greater than 2 ( Means minimum March), from the end of the body of the function it calls a function ‘leap()’. It returns ‘1’ if the year is leap year other wise returns ‘0’.

Within ‘show()’ function we initialize a variable ‘x’ with 1 if the day name is Sunday, 2 if the day name is Monday and so on. That means in our BlueJ program ‘x’ holds the day number of the 1st January of the specific year. Then using a for loop, count the total number of days from 1st January to the specified date and ‘x’ is utilized to hold the day number of the specific date.

Related Post: BlueJ Programs on Numeric Array
.

Subscribe via email

Enter your email address:

Delivered by FeedBurner