public class Combolock
{
private int num1;
private int num2;
private int num3;
public Combolock ( int a, int b, int c)
{
num1 = a;
num2 = b;
num3 = c;
}
//returns true if the proper combination is given
public boolean open ( int a, int b, int c)
{
return ( ( a == num1 ) && ( b == num2 ) && (c == num3 ) );
}
public boolean changeCombo( int a, int b, int c, int newA,
int newB, int newC )
{
if ( open( a, b, c) )
{
num1 = newA;
num2 = newB;
num3 = newC;
return true;
}
return false;
}
public static void printArray(int [][] arr)
{
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
System.out.print(String.format("%5d", arr[row][col]));
System.out.println();
}
}
public class BinaryArray
{
private boolean [] arr = null;
public BinaryArray(String init)
{
arr = new boolean[init.length()];
for (boolean value : arr)
value = false;
for (int ch = 0; ch < init.length(); ch++)
{
boolean val = false;
if (init.charAt(ch) == 'T')
val = true;
arr[ch] = val;
}
}
public int size()
{
return arr.length;
}
public String toString()
{
String result = "";
for (int i = 0; i < arr.length; i++)
result += arr[i] + " ";
return result;
}
public boolean get(int location)
{
return arr[location];
}
public void set(int location, boolean value)
{
arr[location] = value;
}
}
Base( int pub, int pri, int pro )
{
bPublic = pub; bPrivate = pri; bProtect = pro;
}
Derived( int bpub, int bpri, int bpro, int dpub, int dpri )
{
super( bpub, bpri, bpro );
dPublic = dpub; dPrivate = dpri;
}
class Person implements Comparable
{
public int compareTo(Object rhs)
{
Person p2 = (Person) rhs;
return (name.compareTo(p2.name));
}
}
/**
* Enum for a baseball player position
*
*/
public enum Player
{
Pitcher, Catcher, Firstbase, Secondbase, Shortstop, Thirdbase, Leftfield, Centerfield, Rightfield;
}
import java.util.*;
/**
* PlayerClient is a driver for the Player enum.
*/
public final class PlayerClient
{
public static void main(String[] args)
{
// show all the players, in order.
for (Player p: Player.values())
{
System.out.println(p);
}
//Read a string from the user that represents a player, and creates an enum value.
Scanner console = new Scanner(System.in);
String input = console.nextLine();
try
{
Player player = Player.valueOf(input);
// If the enum value is a Pitcher, print "Strike", otherwise print "Ball".
if (player.equals(Player.Pitcher))
{
System.out.println("Strike");
}
else
{
System.out.println("Ball");
}
}
catch (IllegalArgumentException ex)
{
System.out.println("Input is not a player.");
}
}
}
Problem 1 Solution
a) Only the Private fields can not be accessed.
b) Only g.gPrivate is illegal.
c) Only a.aPrivate is illegal.
d) The same as previous, only a.aPrivate is illegal.
e) Access to Gala is illegal. Only the Public fields can be
accessed.
f) Only aLimited is illegal.
g) Place the statements below in the public section of their
respective classes;
h) aPrivate is the only member not accessible in Gala.
Apple( int pub, int pri, int pro, int lim )
{
aPublic = pub; aPrivate = pri; aProtect = pro; aLimited = lim
}
Gala( int apub, int apri, int apro, int gpub, int gpri )
{
super( apub, apri, apro );
gPublic = gpub; gPrivate = gpri;
}
public static int count(
Collection<Collection<String>> c, String str )
{
int count = 0;
// go through each String object
// in each Collection object in
Collection c
for( Collection strC : c )
{
for( Object
o : strC )
{
String s = (String) o;
if ( s.equals( str ) )
{
count++;
}
}
}
return count;
}
Iterator<Integer> iter = rhs.iterator();
for( Integer
num : lhs)
if( !num.equals( iter.next() ) )
return false;
| bird |
mouse |
fox |
| bird |
| cat |
| dog |
| 4 5 8 |
9 8 |
72 |
7 72 |
65 |
0 1 105 2 639 3 718 4 692 5 538 6 799 7 254 8 277 9 512 10 101 11 375 12 822b.
0 277 1 2 639 3 718 4 692 5 538 6 800 7 254 8 255 9 100 10 101 11 375 12
public static long sumOfDigits(long x)
{
if (x < 10)
{
return x;
}
return (x % 10) + sumOfDigits( x / 10 );
}
| Parent |
Children |
Siblings |
Height |
Depth |
Size |
|
| A |
B,C |
4 |
0 |
13 |
||
| B |
A |
D,E |
C |
3 |
1 |
9 |
| C |
A |
F |
B |
2 |
1 |
3 |
| D |
B |
G,H |
E |
1 |
2 |
3 |
| E |
B |
I,J |
D |
2 |
2 |
5 |
| F |
C |
K |
1 |
2 |
2 |
|
| G |
D |
H |
0 |
3 |
1 |
|
| H |
D |
G |
0 |
3 |
1 |
|
| I |
E |
J |
0 |
3 |
1 |
|
| J |
E |
L,M |
I |
1 |
3 |
3 |
| K |
F |
0 |
3 |
1 |
||
| L |
J |
M |
0 |
4 |
1 |
|
| M |
J |
L |
0 |
4 |
1 |
Problem 1 A B -> D B A -> C C B -> D -> G D A -> C -> E -> F E D -> F F D -> E G C Problem 2 A B C D E F G A 1 1 B 1 1 C 1 1 1 D 1 1 1 1 E 1 1 F 1 1 G 1 Problem 3 1 2 3 4 5 6 7 1 1 1 1 2 1 1 3 1 4 1 1 1 5 1 1 6 7 1
1. Breadth-first A B I C H D G E F B A C H I D G E F E D C F B G A H I F D G C E H B I A2. Depth first A B C D E F G H I H B A I C D E F G G F D C B A I H E