Tracing Questions
import java.util.*;
public class MethodsToTrace
{
public static void main(){
mysteryRef1();
inheritance();
System.out.println( calcit(3452));
mysteryStack();
mapTrace();
}
public static void mapTrace(){
String key = "";
Map<String,String> productMap = new TreeMap<String,String>();
Map<String,String> itemMap = new HashMap<String,String>();
productMap.put("Z01","ONE");
productMap.put("X02","TWO");
productMap.put("X02","THREE");
productMap.put("Y01","TWO");
itemMap.put("ONE","XX02");
itemMap.put("TWO","XX01");
itemMap.put("THREE","XX09");
Set productMapKey = productMap.keySet();
for (String s : productMap.keySet()){
System.out.println(itemMap.get(productMap.get(s)));
}
}
public static int calcit(int num)
{
if (num / 10 == 0) {
return num;
}
int lastDigit = num %10;
if (lastDigit %2 == 0 ) {
return 2 * calcit(num /10);
}
else {
return lastDigit + calcit(num/10);
}
}
public static void mysteryStack(){
Stack<Integer> s = new Stack<Integer>();
System.out.println(s.isEmpty()+",");
s.push(535);
System.out.println(s.pop()+",");
s.push(335);
System.out.println(s.peek()+",");
System.out.println(s.size()+",");
s.push(29);
s.push(-378);
Queue<Integer> q = new LinkedList<Integer>();
while (!s.isEmpty()) {
int n = s.pop();
q.add(n);
q.add(n);
}
while (!q.isEmpty()) {
s.push(q.remove());
}
System.out.println(s);
}
public static void mysteryRef1( ) {
String name = "Mary";
int money = 308;
Account a = new Account(name, money);
money = mysteryRef2(a, money);
money = money + 100;
name = "Mary";
System.out.println("" + money + a);
}
public static int mysteryRef2(Account anAcc, int money) {
anAcc.addMoney(10);
money = money + 250;
return 70;
}
public static void inheritance( ) {
Parent p = new Child("harry", 8);
p.doSomething();
p.doprintln();
p.doTwo();
Child c = new Child("rugrat",5);
c.doSomething();
c.doprintln();
c.doSomethingJustChild();
c.doTwo();
Parent p2 = new Cousin("kin");
p2.doSomething();
p2.doprintln();
// why no
// p.doSomethingJustChild()
// p2.doSomethingJustChild()
}
}
And you have these classes:
public class Account {
String name;
int money;
public
Account(String name, int money) {
this.name = name;
this.money
= money;
}
public
void addMoney(int n){
this.money
= this.money + 1; }
public
String toString() {
return
name + ":" + money;
}
}
public class Child extends Parent
{
int age;
public
Child(String name, int age){
super(name);
this.age
= age;
}
public void doSomething(){
super.doSomething();
System.out.println("kid"
+ this.name + " age " + age);
}
public
void doSomethingJustChild(){
System.out.println("just
child");
super.doSomething();
}
public void doTwo(){
System.out.println(this.name
+ "," + this.age);
}
}
public abstract class Parent
{ String name;
public
Parent(String name){
this.name = name;
}
public
void doSomething(){
System.out.println(this.name);
}
public
void doprintln(){
System.out.print("big");
}
public
abstract void doTwo();
}
public class Cousin extends Parent
{
public
Cousin(String name ){
super(name);
}
public void doTwo(){
System.out.println(this.name
+ "," + "Cousin");
}
}