class
extend
another class
?
class
implement
an interface
?
interface
extend
another interface
?
class
implement
more than one interface
?
Animation
class:
public class Animation { public void play() { System.out.println("I don't know how!"); } }
and the following application:
public class Bad { public static void main(String[] arguments) { int i; if ((arguments.length > 0) || (arguments != null)) { System.out.println(arguments[0]); } i = 1; j = 2; Animation.play(); } }
Animation
class is correct,
identify all of the syntactic defects (i.e., the defects that
will be detected at compile-time) in the Bad
class.
Bad
class.
Pair
class:
public class Pair { public double x, y; }
Which lines in the following class will compile and which lines won't?
public class WereYouPayingAttention { private final int i = 0; private final boolean[] b = new boolean[2]; private final Pair p = new Pair(); public void change() { i = 5; b[0] = true; p.x = 2.0; p = new Pair(); p = null; b = new boolean[2]; b = new boolean[3]; } }
Security
and Stock
classes:
public abstract class Security { protected double price; private int volume; public Security() { price = 100.0; volume = 5000; } public abstract void print(); }
public class Stock extends Security { private String exchange; public Stock() { super(); price = 200.0; exchange = new String("NASDAQ"); } public void print() { System.out.println(price+" "+exchange); } }
What will be printed by the following code snippet?
public class Question { public static void main(String[] args) { Stock s; s = new Stock(); s.print(); } }
public class Chirp {...}
public class ExpandedChirp extends Chirp {...}
and the following methods in another class:
public void add(Chirp ch) {...}
public void add(ExpandedChirp ch) {...}
add()
is passed a
Chirp
object?
add()
is passed an
ExpandedChirp
object
that is declared to be an ExpandedChirp
>
add()
is passed an
ExpandedChirp
object that is declared to be
a Chirp
?
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"); } }
Clock
class:
public abstract class Clock { protected int hours, minutes; public Clock() { this(0,0); } public Clock(int hours, int minutes) { this.hours = hours; this.minutes = minutes; } public abstract String format(); public void print() { System.out.println(format()); } }
the following TerseClock
class:
public class TerseClock extends Clock { public TerseClock(int hours, int minutes) { super(hours, minutes); } public String format() { return "" + hours + ":" + minutes; } }
and the following VerboseClock
class:
public class VerboseClock extends Clock { public VerboseClock(int hours, int minutes) { super(hours, minutes); } public String format() { return "The time is " + minutes +" after " + hours; } }
either identify and fix all of the errors
in the following ClockDriver
class
or show what will be printed (assuming it is executed properly):
public class ClockDriver { public static void main(String[] args) { Clock c; c = new TerseClock(10,15); c.print(); c = new VerboseClock(12,30); c.print(); } }
Taxable
interface,
and Book
, Clothes
, Food
,
and TaxBasedOnPrice
classes:
public interface Taxable { public abstract double getTax(); }
public class Book implements Taxable { private String title; public Book(String title) { this.title = title; } public double getTax() { return 5.00; } public String toString() { return title + " Tax: " + getTax(); } }
public class Clothes extends TaxBasedOnPrice { private int size; private String color, style; public Clothes(String style, String color, int size, double price) { super(price, 0.10); this.style = style; this.color = color; this.size = size; } public String toString() { return "Size " + size + " " + style + " in " + color + " Price: " + getPrice() + " Tax: " + getTax(); } }
public class Food extends TaxBasedOnPrice { private String description; public Food(String description, double price) { super(price, 0.05); this.description = description; } public String toString() { return description + " Price: " + getPrice() + " Tax: " + getTax(); } }
public abstract class TaxBasedOnPrice implements Taxable { private double price, taxRate; public TaxBasedOnPrice(double price, double taxRate) { this.price = price; this.taxRate = taxRate; } public double getPrice() { return price; } public double getTax() { return price * taxRate; } }
what will be printed by the following driver?
public class TaxDriver { public static void main(String[] args) { Book book; Clothes clothes; Food food; Taxable[] purchases; purchases = new Taxable[3]; book = new Book("Multimedia Software"); clothes = new Clothes("Shirt", "Yellow", 8, 12.00); food = new Food("Hot Dog",2.00); purchases[0] = book; purchases[1] = clothes; purchases[2] = food; printMessage(food); printMessage(purchases[0]); printMessage(clothes); printMessage(purchases[1]); System.out.println(book.getTax()); System.out.println(clothes.getTax()); System.out.println(food.getTax()); for (int i=0; i<purchases.length; i++) System.out.println(purchases[i].getTax()); printAll(purchases); } public static void printMessage(Food food) { System.out.println("I'm food!"); } public static void printMessage(Taxable taxable) { System.out.println("I'm taxable!"); } public static void printAll(Taxable[] items) { for (int i=0; i<items.length; i++) System.out.println(items[i].getTax()); for (int i=0; i<items.length; i++) System.out.println(items[i].toString()); } }
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
?
public interface Sized { public int getSize (); }
public class Business implements Sized { public String name; public int squareFeet; public Business ( String name , int squareFeet ) { this.name = name; this.squareFeet = squareFeet; } public int getSize () { return squareFeet; } public String toString () { return name + " (" + squareFeet + " sq. feet )"; } }
public abstract class Residence implements Sized { private int squareFeet; public Residence ( int squareFeet ) { this.squareFeet = squareFeet; } public abstract int payment (); public int getSize() { return squareFeet; } public String toString() { return " Square feet : " + getSize () + " Payment : " + payment (); } }
public class Apartment extends Residence { private int rent; public Apartment (int rent ,int squareFeet) { super(squareFeet); this.rent = rent ; } public int payment() { return rent; } }
public class Condo extends Residence { private int mortgage; public Condo(int mortgage, int squareFeet) { super(squareFeet); this.mortgage = mortgage ; } public int payment () { return mortgage ; } }
and the following declarations and instantiations:
Sized[] stuff ; Residence[] homes ; Condo[] development ; stuff = new Sized[3]; homes = new Residence[3]; development = new Condo[3];
indicate whether each of the following statements will: not compile (N), compile but generate an exception at runtime (C+X), or compile and run without generating an exception (C+R).
stuff[0] = new Business("Martins", 12000); stuff[1] = new Residence(2200); stuff[2] = new Condo(900, 2200); stuff[0] = homes[0]; homes[0] = stuff[0]; stuff[0] = development[0]; homes[0] = new Business("Kroger", 10000); homes[1] = new Residence(2200); homes[2] = new Apartment(600, 1200); development[0] = new Condo(600, 1200); stuff[0].name = "Food Lion"; development[0].squareFeet = 200; int p = homes[0].payment(); int q = development[0].payment(); int m = development[0].mortgage; int s = stuff[0].getSize(); int t = homes[0].getSize(); int u = development[0].getSize();
downtown
and region
as ArrayLists
of Business
objects.
downtown
.
hburg
as a HashMap
in which the keys are String
objects and
downtown
could be a value.
hburg
.
downtown
to hburg
using the key
"Downtown"
.
iter
as an Iterator
of Business
objects.
hburg
with the key "Downtown"
and assign it to the variable named region
.
Iterator
of Business
objects from
region
and assign it to iter
.
sum
and initialize it to
0
.
iter
to write a loop that adds up the total
square footage of all of the Business
objects from
region
. The result must be stored in
sum
. (Multiple statements.)
true
when the parameter
age
is in the closed interval \([13, 19]\) and
must return false otherwise. Implement this method in one statement,
without using an if
statement, a ternary operator,
loop, or method call.
public static boolean isTeenager(int age) { }
Copyright 2021