Você está na página 1de 11

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

Problem 1. Short answer problems.


1. What is displayed on the console when the following program is run?
public class Test {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int[] a = new int[10];
a[10] = 1;
System.out.println("Welcome to HTML");
}
catch (Exception ex) {
System.out.println("There is an exception);
}
System.out.println("Bye.");
}
}

2. ____________ sets a background of a JPanel object p to yellow.


a.
b.
c.
d.

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.

g.drawCircle(350, 350, 100, 100)


g.drawOval(350, 350, 100, 100)
g.drawCircle(400, 400, 50, 50)
g.drawOvale(400, 400, 50, 50)

Page 1 of 11

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

4. Assume class B extends class C and implements interface A. What will be printed by

the following program?


public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}

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.

public class Test {


public static Object max(Object o1, Object o2) {
if ( ((Comparable)o1).compareTo(o2) >= 0)
return o1;
else
return o2;
}
}

Page 2 of 11

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

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;}
}

/* Class TournamentHistory represents a set of team results for a


tournament. */
import java.util.ArrayList;
public class TournamentHistory {
private String name;
private ArrayList<Team>

teamData = new ArrayList<Team>(5);

public TournamentHistory (String tournamentName){


this.name = tournamentName;
}
public void addTeamRecord ( String name,

int year,

int place ){

boolean found = false;


for (int i = 0; i< teamData.size()&&found == false; i++){
Team tm = teamData.get(i);
if ( tm.getName().equalsIgnoreCase(name) &&
tm.getYear() == year)
found = true;
}

Page 3 of 11

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

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

CS280 Object-Oriented Programming-Fall12


b)

Final Exam Practice Problems

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

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

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

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

d) Complete the definition of a static method readFromFile below of the


TournamentHistory class to create and return an object of type
TournamentHistory, filled with data from file named data.txt that has the
following format (illustrated in the box below):
1. The first line contains the name of the tournament
2. The rest of lines contain team data, including year, team name and the place number:
Sample file data.txt:
FIFA World Cup
1998 Croatia 3
1998 France 1
1998 Brazil 2
2002 Brazil 1
HINT: Use methods of the Scanner class to read data from the file,
// this is part of class

TournamentHistory

public static TournamentHistory readFromFile(){


TournamentHistory th;

// variable to store the object


// that this method will create and
// return.

Page 7 of 11

CS280 Object-Oriented Programming-Fall12

return th;
}

Page 8 of 11

Final Exam Practice Problems

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

Problem 3. GUI with Swing (25 points)


The code displayed on the next page, when run, displays the following window. Answer the
questions that follow.

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).

b) What will happen when a user presses on the Exit button?

c) Which method is being invoked by the call


super("Currency converter");

and what does that method do?

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

CS280 Object-Oriented Programming-Fall12

Final Exam Practice Problems

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

JTextField usValue = new JTextField(10);


JTextField caValue = new JTextField(10);
JButton bConvert = new JButton("Convert");
JButton bExit = new JButton("Exit");

/**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

CS280 Object-Oriented Programming-Fall12

}
Page 11 of 11

Final Exam Practice Problems

Você também pode gostar