Skip to content

Instantly share code, notes, and snippets.

@effective-light
Last active June 20, 2017 07:11
Show Gist options
  • Save effective-light/a0c31df9596a846a0e8ece50ed9bd770 to your computer and use it in GitHub Desktop.
Save effective-light/a0c31df9596a846a0e8ece50ed9bd770 to your computer and use it in GitHub Desktop.
Answers to CSC207H1Y Quiz 1 V1
package network;
public class LocalAreaNetwork extends Network
{
public static String whatAmI = "I am a LAN";
public static int numCreated;
public int numNodes;
public char firstLetter = 'L';
public String location;
public LocalAreaNetwork(int numNodes, String location)
{
super( numNodes );
this.location = location;
numCreated++;
}
@Override
public String toString()
{
return firstLetter + " is for LAN";
}
public boolean equals(Network x)
{
return x instanceof LocalAreaNetwork && x.numNodes == this.numNodes;
}
}
package network;
public class Network
{
public static String whatAmI = "I am a Network";
public static int numCreated;
public int numNodes;
public char firstLetter = 'N';
public Network(int numNodes)
{
this.numNodes = numNodes;
numCreated++;
}
public String printNumNodes()
{
return "The number of nodes in this network is " + numNodes;
}
@Override
public String toString()
{
return firstLetter + " is for Network";
}
public boolean equals(Network x)
{
return x instanceof Network;
}
}
package network;
public class Quiz1Demo
{
private static int numQuestion;
public static void main(String[] args)
{
Network n1 = new Network( 200 );
Network n2 = new LocalAreaNetwork( 300, "UTM" );
LocalAreaNetwork n3 = new LocalAreaNetwork( 400, "UTSC" );
// Q1
println( Network.numCreated );
// Q2
println( LocalAreaNetwork.numCreated );
// Q3
println( n3.location );
// Q4
// println( n2.location );
println( "error: cannot find symbol" );
// Q5
println( n1.numNodes );
// Q6
println( n2.numNodes );
// Q7
println( n3.numNodes );
// Q8
println( n1 );
// Q9
println( n2 );
// Q10
println( n3 );
// Q11
println( n1.printNumNodes() );
// Q12
println( n3.printNumNodes() );
// Q13
println( n1.equals( n2 ) );
// Q14
println( n2.equals( n3 ) );
// Q15
println( n3.equals( n2 ) );
// Q16
// Proof by example. Since, the question asks: If at least one instance where they are the "same" while their locations differ exists.
println( n3.equals( new LocalAreaNetwork( 0, "some location" ) ) );
// Q17
println( "no" ); // By logic
Integer x = new Integer( 3 );
int y = 4;
Integer z = x + y;
int q = x + y;
// Q18
println( z == q );
// Q19
println( z.equals( q ) );
// Q20
// println( q.equals( z ) );
println( "error: int cannot be dereferenced" );
}
private static<T> void println(T arg)
{
numQuestion++;
System.out.println( "Question #" + numQuestion );
System.out.println( arg );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment