Escolar Documentos
Profissional Documentos
Cultura Documentos
p.setColor(Color.yellow)
p.setBackground(Color.yellow)
p.setBackgroundColor(Color.yellow)
p.setBackGroundColor(Color.yellow)
3. _______________ draws a circle centered at position is (400, 400) with radius 50.
e.
f.
g.
h.
Page 1 of 11
4. Assume class B extends class C and implements interface A. What will be printed by
5. The class Test below compiles without error, however, method max that is defined in
it can sometimes generate a run-time error.
Explain
a. Why the cast of o1 to Comparable is necessary for this code to compile without
error.
b. When and why a call to method max would result in a run-time error.
Page 2 of 11
Problem 2.
The questions that follow are based on the definitions of classes Team and
TournamentHistory below. These classes represent details of team performance in a
tournament. Review these class definitions to answer the questions that follow.
/* Class Team represents a Teams result in a particular year */
public class Team {
private String name;
private int year; // year of participation
private int place;
public Team (String name, int year, int place){
this.name = name;
this.year = year;
this.place = place;
}
public String toString (){
return this.name + " took " + this.place +
" in " + this.year;
}
public String getName(){return this.name;}
public int getYear(){return this.year;}
public int getPlace(){return this.place;}
}
int year,
int place ){
Page 3 of 11
if (found == true)
System.out.println ("Record of team " + name +
" for year " + year + " exists.");
else {
Team teamRec = new Team (name, year, place);
teamData.add(teamRec);
System.out.println (" Added " + teamRec);
}
}
public static void main (String [] args ){
TournamentHistory fifaWC = new TournamentHistory ("FIFA
World Cup");
fifaWC.addTeamRecord("West Germany", 1974, 1);
fifaWC.addTeamRecord("Poland", 1974, 3);
fifaWC.addTeamRecord("Poland", 1974, 4);
fifaWC.addTeamRecord("West Germany", 1982, 2);
}
}
a) What will be printed when the main method in the TournamentHistory class is
executed? Note that there are 2 printing statements; both are in method
addTeamRecord of the TournamentHistory class. Show the output in the space
provided below.
Page 4 of 11
In the space provided below, redefine the constructor of the Team class to throw an
exception of type Exception in the following cases
1. The name parameter is null or is equal to an empty string. In such case throw an
exception with message Team name cannot be blank.
2. The year parameter is <= 0 or is greater than the current year ( for full credit, use the
Date class to obtain the current year; for partial credit just use 2010). In this case the
exception message should be "Invalid year".
Otherwise, the constructor should assign instance variables as done by the original Team
constructor on page 4.
Page 5 of 11
c) When the Team constructor is redefined to throw an exception as requested in part b),
the following code segment from method addTeamRecord of the
TournamentHistory class would not compile due to a potential exception:
// this code is from method addTeamRecord
if (found)
System.out.println ("Record of team " + name +
" for year " + year + " exists.");
else {
Team teamRec = new Team (name, year, place);
teamData.add(teamRec);
System.out.println (" Added " + teamRec);
}
Rewrite the code above to handle the exception thrown by the Team constructor, so
that when an exception is caught, the exceptions message is displayed to the user
and nothing is added to the teamData arraylist.
Page 6 of 11
TournamentHistory
Page 7 of 11
return th;
}
Page 8 of 11
a) The GUI is designed in such a way that the user cannot enter a value into the grayed
area underneath the Canadian Dollars. Which line of code is responsible for
ensuring this (write down your answer here).
d) Make modifications to the program on the next page so that the it works as
follows:
a. When the Exit button is pressed, the program terminates.
b. When the Convert button is pressed, if there is nothing in the first text
field, a message should be displayed to the user to enter the US dollar
amount. Otherwise (in case the US dollar amount is specified), the amount
of Canadian dollars corresponding to the US Dollar amount specified in
the first text field should be computed and displayed in the second text
field.
You do not have to implement any exception handling for this problem.
Page 9 of 11
import javax.swing.*;
import java.awt.*;
public class TestActionEvent extends JFrame
{
private final double exchangeRate = 1.02;
1.02 US
private
private
private
private
// 1 Canadian is worth
/**Main method*/
public static void main(String[] args) {
TestActionEvent frame = new TestActionEvent();
}
public TestActionEvent()
{
super("Currency converter");
// Use FlowLayout manager to arrange the components
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
// Add labels and text fields
JLabel us = new JLabel ("US Dollars");
JLabel canadian = new JLabel ("Canadian Dollars");
contentPane.add(us); contentPane.add(usValue);
contentPane.add(canadian); contentPane.add(caValue);
caValue.setEditable(false);
// Add buttons
contentPane.add(bConvert);
contentPane.add(bExit);
this.setSize(200, 150);
this.setVisible(true);
Page 10 of 11
}
Page 11 of 11