Bluej Menu Driven Program

Only available on StudyMode
  • Topic: Addition, Perfect number, Number theory
  • Pages : 8 (1040 words )
  • Download(s) : 457
  • Published : December 10, 2012
Open Document
Text Preview
Menu Driven BlueJ Program on Odd, Even and Perfect Numbers

The question here is that,  Write a BlueJ program which will ask the user to enter a choice and based on the choice the following operation will take place. if choice=1; then sum of even nos. from the series of 10 nos. if choice=2; then sum of odd nos. from a series of 10 nos.

if choice=3; then it will check whether the no is perfect or not from a series of 10 nos.

Codes of the Menu driven BlueJ Program

import java.io.*;
class Menu
{
  BufferedReader br;
int arr[]=new int[10];
   public static void main(String args[])throws IOException    {
  Menu ob=new Menu ();
 ob.accept();
   }
  Menu ()
  {
   br=new BufferedReader(new InputStreamReader(System.in));   }
public void accept()throws IOException
{
int choice;
for(int i=0;i<10;i++)
{
 System.out.println("Enter number:");
 arr[i]=Integer.parseInt(br.readLine().trim());
}
System.out.println("Enter Your Choice (1 for adding even numbers, 2 for adding odd number and 3 for checking perfect number:");  choice=Integer.parseInt(br.readLine().trim());
if(choice==1)
addEven();
else if(choice==2)
addOdd();
else if(choice==3)
showPerfect();
else
System.out.println("Wrong Choice:");
}
private void addEven()
{
 int sum=0;
 for(int i=0;i<10;i++)
 {
     if(arr[i]%2==0)
     sum=sum+arr[i];
    }
    System.out.println("Sum of Even Numbers="+sum);
}

private void addOdd()
{
 int sum=0;
 for(int i=0;i< 10;i++)
 {
     if(arr[i]%2!=0)
     sum=sum+arr[i];
    }
    System.out.println("Sum of Odd Numbers="+sum);
}
private void showPerfect()
{
 int sum;
 for(int i=0;i<10;i++)
 {
     sum=0;
  for(int j=1;j< arr[i];j++)
 {
     if(arr[i]%j==0)
     sum=sum+j;
    }
    if(sum==arr[i])
    System.out.println(arr[i]+ " is a Prefect Number");
}
}
}

Sample input and output of the BlueJ program

Enter number:
22
Enter number:
4
Enter number:
35
Enter number:
4
Enter number:
32
Enter number:
12
Enter number:
6
Enter number:
5
Enter number:
4
Enter number:
33
Enter Your Choice (1 for adding even numbers, 2 for adding odd number and 3 for checking perfect number: 3
6 is a Prefect Numbers
Enter number:
2
Enter number:
4
Enter number:
33
Enter number:
6
Enter number:
5
Enter number:
45
Enter number:
7
Enter number:
12
Enter number:
2
Enter number:
3
Enter Your Choice (1 for adding even numbers, 2 for adding odd number and 3 for checking perfect number: 1
Sum of Even Numbers=26

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
{...
tracking img