SoundStage?
parseDouble
method in the Double class? Can you call the
parseDouble method outside of a
try-catch block? Why or why not?
(5 % 3) + (8 / 16)
|
(1) |
(0 < 5 & & < 100)
|
(2) |
7 == (5 > 2)
|
(3) |
!(5 == ((5 / 2) * 2))
|
(4) |
| (1) | _____ |
A class is: |
|
||
| (2) | _____ |
Information hiding: |
|
||
| (3) | _____ |
Specialization is often described as the: |
|
||
| (4) | _____ |
A private variable in the Java class Story
is visible/accessible to: |
|
||
| (5) | _____ |
Abstraction involves: |
|
LetterGrade contains the following:
public enum LetterGrade
{
F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;
}
LetterGrade
named eng299?
LetterGrade
corresponding to an "A-" to eng299?
LetterGrade objects
names eng299 and cs239?
Animation class:
public class Animation
{
public void play()
{
System.out.println("I don't know how!");
}
}
Identify the problem(s) with the following code snippet:
public class Bad
{
public static void main(String[] args)
{
int i;
i = 7;
if (args[0] != null) System.out.println(args[0]);
Animation.play();
}
}
public class StudyAid
{
public static void main(String[] args)
{
int denominator, numerator, ratio;
numerator = 5;
denominator = 0;
ratio = numerator / denominator;
System.out.println("The answer is: "+ratio);
}
}
Security, Stock,
and Question classes:
public class Security
{
protected double price;
private int volume;
public Security()
{
price = 0.0;
volume = 0;
}
public String toString()
{
String value;
value = "Price: "+price+" Volume: "+volume;
return value;
}
}
public class Stock extends Security
{
private String exchange;
public Stock()
{
super();
price = 200.0;
volume = 1000;
exchange = new String("NASDAQ");
}
public void toString()
{
String value;
value = super.toString() + " Exchange: " + exchange;
return value;
}
}
public class Question
{
public static void main(String[] args)
{
Stock hal;
hal = new Stock();
System.out.println(hal.toString());
}
}
What errors will be generated when these classes are compiled?
SoundModifier
class:
public class SoundModifier
{
int b[];
public SoundModifier(int[] a)
{
b = a;
}
public void modify()
{
int i;
for (i=0; i<b.length; i++)
{
b[i] = i*2;
}
}
}
What will be printed by the following code snippet?
public class Question
{
public static void main(String[] args)
{
int i;
int[] tune;
SoundModifier m;
tune = new int[5];
for (i=0; i<5; i++)
{
tune[i] = 100;
}
m = new SoundModifier(tune);
m.modify();
for (i=0; i<5; i++)
{
System.out.println(tune[i]);
}
}
}
RunningTime and Movie
classes:
public class RunningTime
{
public double hours, minutes;
public RunningTime(double hrs, double mins)
{
hours = hrs;
minutes = mins;
}
}
public class Movie
{
public RunningTime rt;
public Movie(RunningTime length)
{
rt = length;
}
}
What will be printed by the following code snippet?
public class Theater
{
public static void main(String[] args)
{
Movie matrix;
RunningTime rtMatrix;
rtMatrix = new RunningTime(2.0, 10.0);
matrix = new Movie(rtMatrix);
rtMatrix.hours = 5.0;
System.out.println(matrix.rt.hours+"hrs "+
matrix.rt.minutes+"mins");
}
}
Modem and SlowModem
classes:
/**
* A partial implementation of a modem
*/
public class Modem
{
protected int duplex, speed;
/**
* Default Constructor
*/
public Modem()
{
speed = 38400;
duplex = 1;
}
/**
* Gets the duplex
*
* @return The duplex
*/
public int getDuplex()
{
return duplex;
}
/**
* Gets the speed of the modem (in bits per second)
*
* @return The speed
*/
public int getSpeed()
{
return speed;
}
}
/**
* A partial implementation of a modem
*
* This version is slow
*/
public class SlowModem extends Modem
{
/**
* Default Constructor
*/
public SlowModem()
{
super();
duplex = duplex / 2;
}
/**
* Gets this speed
*
* @return The speed
*/
public int getSpeed()
{
speed = super.getSpeed() / 2;
return speed;
}
}
either identify all of the errors in the following driver (if there are any) or show what the driver will print when it is executed.
public class ModemDriver
{
public static void main(String[] args)
{
Modem fast;
SlowModem slow;
fast = new Modem();
slow = new SlowModem();
System.out.println("Before:");
System.out.println(" Normal: Speed = "+fast.getSpeed());
System.out.println(" Normal: Duplex = "+fast.getDuplex());
System.out.println(" Slow: Speed = "+slow.getSpeed());
System.out.println(" Slow: Duplex = "+slow.getDuplex());
System.out.println("After:");
System.out.println(" Normal: Speed = "+fast.getSpeed());
System.out.println(" Normal: Duplex = "+fast.getDuplex());
System.out.println(" Slow: Speed = "+slow.getSpeed());
System.out.println(" Slow: Duplex = "+slow.getDuplex());
}
}
Contact class:
public class Contact
{
private int extension;
private String[] info;
public Contact(String[] info, int extension)
{
this.info = info;
this.extension = extension;
}
public String toString()
{
String result;
result = "Contact:\n";
for (int i=0; i<info.length; i++)
{
result += info[i] + "\n";
}
result += "x" + extension + "\n";
return result;
}
}
what will be printed by the following code snippet? (Note: Be careful!)
public class Driver
{
public static void main(String[] args)
{
Contact[] contacts;
String[] text;
contacts = new Contact[3];
text = new String[2];
text[0] = new String("D. Bernstein");
text[1] = new String("Room 257");
contacts[0] = new Contact(text, 1671);
System.out.println(contacts[0].toString());
text[0] = new String("N. Harris");
text[1] = new String("Room 217");
contacts[1] = new Contact(text, 8771);
System.out.println(contacts[1].toString());
text[0] = new String("M. Norton");
text[1] = new String("Room 209");
contacts[2] = new Contact(text, 2777);
System.out.println(contacts[2].toString());
System.out.println("Everybody");
for (int i=0; i<contacts.length; i++)
{
System.out.println(contacts[i].toString());
}
}
}
A combination lock is either "locked" or "unlocked". A combination lock has an associated combination (i.e., a sequence of 1 or more characters) that cannot be changed. A user can enter a combination; if the combination is correct then the combination lock becomes "unlocked". A user can also "lock" a combination lock.
/**
* Adds the numbers entered at the command line.
*
* If one of the "numbers" is, in fact, not a number
* it should be ignored.
*
*/
public class Adder
{
public static void main(String[] args)
{
}
}
Note: If this application is executed from the command line as follows:
java Adder 10 50 bob 40
it should print out:
100
Score class:
public class Score
{
public int score;
public String name;
/**
* Construct a new Score
*
* @param score The number of points in this Score
* @param name The person that got this score
*/
public Score(int score, String name)
{
this.score = score;
this.name = name;
}
}
Complete the insert method in the
HighScoreList class below:
public class HighScoreList
{
private Score[] scores;
/**
* Construct a new HighScoreList
*/
public HighScoreList()
{
int i;
scores = new Score[10];
for (i=0; i<10; i++)
{
scores[i] = new Score(0," ");
}
}
/**
* Insert the given score into the list if
* it is appropriate to do so
*
* @param newscore The Score to consider
* @return true if the score was inserted
*/
public boolean insert(Score newscore)
{
}
}
Review class contains the following methods
(properly implemented):
/**
* A Review object is an encapsulation of a movie review
*/
public class Review
{
/**
* Explicit Value Constructor
*
* @param words The words in this Review object
*/
public Review(String[] words)
/**
* Determine if this word is a registered trademark
*
* @param word The word to check
* @return true if the word is a trademark
*/
protected boolean isTrademark(String word)
/**
* Get a particular word
*
* @param index The word number
*/
protected String getWord(int index)
/**
* Get the total number of words in this Review object
*
* @returns The number of words
*/
public int getNumberOfWords()
/**
* Set the words contained in this Review
*
* @param words The words
*/
private void setText(String[] words)
}
complete the following PrintReadyReview
class. [Notes: (1) Your implementation must be consistent with the
comments. (2) Make sure you understand the capabilities of the
Review class before you start writing this class.]
/**
* A specialized Review that is ready to be printed.
* Specifically, it knows how to create a String containing
* all of the words formatted into lines.
*/
public class PrintReadyReview extends Review
{
private int width;
/**
* Explicit Value Constructor - Constructs a PrintReadyReview
* object with a maximum line width of 80 characters.
*
* @param words The words in this PrintReadyReview object
*/
public PrintReadyReview(String[] words)
{
}
/**
* Set the maximum line width (in characters)
*
* @param width The maximum line width
*/
public void setMaximumWidth(int width)
{
}
/**
* Returns a String representation of this PrintReadyReview.
* This String contains a ' ' character between words
* and a '\n' character at the end of each line. The
* length() method in the String class is used to determine
* the number of characters in each word. Each line contains
* as many characters as possible, but not more than width.
* Also, lines do not break in the middle of a word.
*
* The String "(R)" is inserted after each word that is a
* registered trademark.
*/
public String toString()
{
}
}
Copyright 2010