| Term Exam 2 | : Monday March 13th, 2000 |
| Instructor | : Osmar Zaïane |
| Section | : B1 |
| Version | : A |
switch(a) {
case 2: b=10; break;
case 4:
case 7: b = 8;
case 9: b=10;break;
default: b = 9;
}
a) 9;
for(i=0; i<5; i++) {
j=i;
while (j<5) {
System.out.print("*");
j=j+1;
}
}
a) 5;
|
public double newSum (double sum) { if (sum < 200) return sum; else if (sum > 400) return (sum * 0.8); else return (sum * 0.95); } |
|
public class CompactDisc { /*Instance Variables*/ private String title; private double value; /*Constructor*/ public CompactDisc(){ System.out.print("Title? "); this.title=Keyboard.in.readString(); System.out.print("Value? "); this.value=Keyboard.in.readFloat().doubleValue(); } /*Instance Methods*/ public double getValue(){ return this.value; } public String getTitle(){ return this.title; } } |
|
import java.util.*; public class CollectionCD { /*Instance Variables*/ private Vector collection; /*Constructor*/ public CollectionCD(){ this.collection = new Vector(); } public void addCD(CompactDisc myCD){ this.collection.addElement(myCD); } public double getTotalValue(){ double totalValue=0; CompactDisc myCD; int i=0; while (i<this.collection.size()) { myCD=(CompactDisc)this.collection.elementAt(i); totalValue = totalValue + myCD.getValue(); i=i+1; } return totalValue; } public int nbValuable(double value){ CompactDisc myCD; int valuableCDs=0; int i=0; while (i<this.collection.size()) { myCD=(CompactDisc)this.collection.elementAt(i); if (myCD.getValue() > value) valuableCDs = valuableCDs + 1; i=i+1; } return valuableCDs; } public void display(){ CompactDisc myCD; int i=0; double theValue; while (i<this.collection.size()) { myCD=(CompactDisc)this.collection.elementAt(i); System.out.print(myCD.getTitle()); System.out.print(" $"); theValue=Math.round(myCD.getValue()*100)/100; System.out.println(theValue); i=i+1; } } } |
|
public class myTest { /* Reads the number of CDs in collection then iterates to read all CDs and their values and displays the whole collection and the total value */ public static void main (String args[]) { int nbCDs; CollectionCD myCDs; int i; double collectionValue; myCDs = new CollectionCD(); System.out.print("How many CDs? "); nbCDs = Keyboard.in.readInteger().intValue(); for(i=0;i<nbCDs;i++){ myCDs.addCD(new CompactDisc()); } myCDs.display(); System.out.print("Total Value=$"); collectionValue=Math.round(myCDs.getTotalValue()*100)/100; System.out.println(collectionValue); System.out.print(myCDs.nbValuable(20)); System.out.println("CDs with value more than $20.00"); } } |
public class myQuestion {
/* instance variables */
private int a;
private int b;
private Stack s;
public myQuestion(int x, int y) {
this.a = x;
this.b = y;
this.s = new Stack();
}
public void a1 (int ms, int mn) {
int a=12;
int b=ms+mn;
if (a<b) a= mn;
else a=ms;
System.out.println(a);
this.a = ms;
this.s.push("ms");
}
public void a2(int ms, int mn) {
int i=0;
while (i<ms) {
this.a=this.a+mn;
this.b=this.b+1;
i=i+1;
}
System.out.println(this.a);
System.out.println(this.b);
this.s.push("mn");
}
public void a3 (int ms, int mn) {
int i;
for(i=ms;i>0;i--) {
if (i<mn) this.a = this.a + 1;
else this.b = this.b + 1;
}
System.out.println(this.a);
System.out.println(this.b);
this.s.push("ab");
System.out.println(this.s);
}
}
|
a ) [10 points] What is the output of the program segment:
myQuestion q; q=new myQuestion(0,0); q.a1(12, 8); q.a1(8,12); q.a2(3,2); q.a3(4,2);
8
12
14
3
15
6
["ms", "ms", "mn", "ab"]
b) [10 points] What is the output of the program segment:
myQuestion q; q=new myQuestion(1,2); q.a1(12, 5); q.a2(1,8); q.a1(10,12); q.a3(5,3);
5
20
3
12
12
6
["ms", "mn", "ms", "ab"]