Professional Documents
Culture Documents
import java.util.*;
class Appliances
{
int cost = 2;
}
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.*;
lblText.setFont(new Font("Serif", Font.BOLD, 20));//set font style and font size of the label
btnDoggy.addActionListener(new ActionListener() {
lblText.setText("Bow Bow");
});
f.add(btnDoggy);
f.add(lblText);
f.setTitle("Doggy");
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.*;
g.setFont(a1);
g.drawString("CODE Applet”,35,45);
}
}
5.
Question
1. impart java.swing.*;
2. import java.awt.*;
4. {
14. {
20. setBorder(BorderFactory.createTitledBorder("Toppings"));
21. add(creamCheese);
22. add(butter);
23. add(peachJelly);
24. add(blueberryJam);
25. }
27. {
29. if (creamCheese.isSelected())
31. if (butter.isSelected())
33. if (peachJelly.isSelected())
38.
39. }
Line Number: 1
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")