You are on page 1of 10

1. Write a java program to create a class named Appliances.

In this class we have three sub classes


Fridge, Machine and Microwave each class has one member function named billCalcuation().
This method can calculate the bill a $2 per unit electricity consumption. Create these using
polymorphism concepts.

import java.util.*;

class Appliances
{
int cost = 2;

void billCalculation(int consumption)


{
int total_cost = cost * consumption;
System.out.println("Total bill is: " + total_cost);
System.out.println();
}
void display()
{
System.out.println("Appliances class-");
}
}

class Fridge extends Appliances


{
void display()
{
System.out.println("Fridge sub class-");
}

class Machine extends Appliances


{
void display()
{
System.out.println("Machine sub class-");
}

class Microwave extends Appliances


{
void display()
{
System.out.println("Microwave sub class-");
}

}
public class Main
{
public static void main(String[] args) {

Appliances a;
a = new Fridge();
a.display();
a.billCalculation(15);

a= new Machine();
a.display();
a.billCalculation(18);

a= new Microwave();
a.display();
a.billCalculation(10);

}
}

2. Write Java code which will create a GUI window on screen. The window has one button: Doggy.
When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax. (10
marks)

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class Doggy {

public static void main(String[] args) {

JFrame f=new JFrame();//creating instance of JFrame

JButton btnDoggy=new JButton("Doggy");//creates the JButton

JLabel lblText=new JLabel();//creates the JLabel

btnDoggy.setBounds(100, 30,110 , 30);//set postion of the button


lblText.setBounds(120, 90,110 , 30);//set postion label

lblText.setFont(new Font("Serif", Font.BOLD, 20));//set font style and font size of the label

//button click event

btnDoggy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

lblText.setText("Bow Bow");

});

f.add(btnDoggy);

f.add(lblText);

f.setSize(350,200);//350 width and 200 height

f.setLayout(null);//using no layout managers

f.setTitle("Doggy");

f.setVisible(true);//making the frame visible

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

3. Write a Java program that asks the user to enter name of any six Days of the week and store
them using an ArrayList. The program should repeatedly ask for name of the day until the user
enters “Exit”. The program then displays the day names on the screen. (10 marks)
import java.util.*;
public class Main
{
public static void main(String[] args) {
ArrayList<String> myList=new ArrayList<String>();
Scanner sc=new Scanner(System.in);
for(;;)
{
System.out.println("input day(exit to end ) ");
String input=sc.nextLine();
if(input.equalsIgnoreCase("end"))
{
break;
}
else if(input.equalsIgnoreCase("sunday") || input.equalsIgnoreCase("monday")||
input.equalsIgnoreCase("tuesday")||input.equalsIgnoreCase("thursday")||
input.equalsIgnoreCase("friday")||input.equalsIgnoreCase("saturday") )
{
myList.add(input);
}
else
System.out.println("Sorry wrong input ");
}
System.out.println(myList);
}
}

4. Write the Java code for an applet that displays the text with Verdana font size 21, “Do not Exit”
inside a red coloured rectangle. Justify the methods used. (10 marks)

import java.awt.*;
import java.applet.*;

public class Sample extends Applet


{
Font a1;

public void init()


{
a1 = new Font("Verdana",Font.BOLD,21); //it will change the font of the
string

public void paint(Graphics g)


{
g.drawString("Do not Exit",30,50);

g.setFont(a1);
g.drawString("CODE Applet”,35,45);

}
}

5.

Question

1. impart java.swing.*;

2. import java.awt.*;

3. public class ToppingPanel extends Panel

4. {

5. public final double CREAM_CHEESE = 0.50;

6. public final double BUTTER = 0.25;

7. public final double PEACH_JELLY = 0.75;

8. public final double BLUEBERRY_JAM = 0.75;

9. private JCheckBox creamCheese;


10. private JCheckBox butter;

11. private JCheckBox peachJelly;

12. private JCheckBox blueberryJam;

13. public ToppingPanel()

14. {

15. setLayout new GridLayout(4, 1));

16. creamCheese = new JCheckBox("Cream cheese");

17. butter = new JCheckBox("Butter");

18. peachJelly = new JCheckBox("Peach jelly");

19. blueberryJam = new JCheckBox("Blueberry jam");

20. setBorder(BorderFactory.createTitledBorder("Toppings"));

21. add(creamCheese);

22. add(butter);
23. add(peachJelly);

24. add(blueberryJam);

25. }

26. public double getToppingCost()

27. {

28. double toppingCost = 0;

29. if (creamCheese.isSelected())

30. toppingCost += CREAM_CHEESE;

31. if (butter.isSelected())

32. toppingCost += BUTTER;

33. if (peachJelly.isSelected())

34. toppingCost += PEACH_JELLY;


35. if (blueberryJam.isSelected())

36. toppingCost += BLUEBERRY_JAM;

37. return toppingCost;

38.

39. }

One error is already identified for you as shown below:

Line Number: 1

Error description: Misspelled import

Statement after correction: import java.swing.*;

Identify 5 other errors and give their Line Number, Error description and Statement after correction.
1) Line no. 1
Description: Misspelled import
2) Line no.1
Description: JCheckBox cannot be resolved to a type error on line
17,19,20,22,28,30,32,34,36,38,40,42,44,46,56,60,64,68 and 75.
Solution: exact error in line no. 1 because we have to import javax.swing.*; to resolve this error.
(you have to import javax.swing not java.swing)
Statement: import javax.swing.*;
3) Line No. 15: In line no 15 you missed 1 left bracket.
Description: Syntax Error on token "setLayout", ( after this token.
Solution: So we need to put the left bracket '('.
Statement: setLayout( new GridLayout(4, 1));
4) Line No. 03: error in line 3 extends JPanel not the Only Panel
Description: The method setBorder is undefined for the type ToppingPanel
Solution: Error is showing on line 20 because the method setBorder is undefined for the type
ToppingPanel:
So we need to import the JPanel not Panel in line no. 03
Statement: public class ToppingPanel extends JPanel
5) Line No. 38: 1 curly bracket is missed after the last line:
Description: Error in line no. 38- Syntax error insert "}" to complete the ClassBody.
Statement: Please find the below code:
6) 1 warning is ToppingPanel does not declare the static final So we need to Suppress the warning
as :
@SuppressWarnings("serial")

CODE: (Error free code)


import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class ToppingPanel extends JPanel
{
    public final double CREAM_CHEESE = 0.50;
    public final double BUTTER = 0.25;
    public final double PEACH_JELLY = 0.75;
    public final double BLUEBERRY_JAM = 0.75;
    private JCheckBox creamCheese;
    private JCheckBox butter;
    private JCheckBox peachJelly;
    private JCheckBox blueberryJam;
    public ToppingPanel()
{
        setLayout( new GridLayout(4, 1));
        creamCheese = new JCheckBox("Cream cheese");
        butter = new JCheckBox("Butter");
        peachJelly = new JCheckBox("Peach jelly");
        blueberryJam = new JCheckBox("Blueberry jam");
        setBorder(BorderFactory.createTitledBorder("Toppings"));
        add(creamCheese);
        add(butter);
        add(peachJelly);
        add(blueberryJam);
}
public double getToppingCost()
{
   double toppingCost = 0;
   if (creamCheese.isSelected())
       toppingCost += CREAM_CHEESE;
   if (butter.isSelected())
       toppingCost += BUTTER;
   if (peachJelly.isSelected())
       toppingCost += PEACH_JELLY;
   if (blueberryJam.isSelected())
       toppingCost += BLUEBERRY_JAM;
   return toppingCost;
}
}

You might also like