CS210 Tutorials: Week 1
The purpose of this tutorial is to revise key concepts learned in the previous course CS102. Complete the following exercises pasted below (or reference to texbook)
You may need to watch these videos to see how to configure Java for command line input.
T1. Download and install JavaQ1. Write a Java program that takes 3 integer values at the command prompt. It prints the three values with their memory addresses. Name the class Q1
Expected input:
java Q1 10 20 30
Expected output:
0x22445512 10
0x2244AC12 20
0x221223F1 30
Expected input:
java -jar Q2 21 Mohamed 22 Ahmed 19 Saleh
Expected output:
0x22445512 21 Mohamed
0x2244AC12 22 Ahmed
0x221223F1 19 Saleh
Expected input:
java -jar Q3 21 Mohamed 22 Ahmed 19 Saleh
Expected output:
0x22445512 21 Mohamed 0x2244AC13
0x2244AC13 22 Ahmed 0x221223F1
0x221223F1 19 Saleh 0x0000FFFF
public class Q1 {
public static void main(String[] args) {
if(args.length!=4)
{
System.out.println("Error: Usage\n Q1 <1st integer> <2nd integer> <3rd integer>");
return;
}
//Note: You cannot get the exact address location for a primitive data-type in java. Using the Integer Wrapper class helps give approximate address location in the Heap memory.
for(int i=1;i<4;i++)
{
Integer x = Integer.parseInt(args[i]);
String address = "0x" + Integer.toHexString(System.identityHashCode(x));
System.out.println(address + " " + x);
}
}
}
class Node{
int age;
String name;
public Node()
{
age = 0;
name = "";
}
}
public class Q2 {
public static void main(String[] args) {
if(args.length!=7)
{
System.out.println("Error: Usage\n Q2 ");
return;
}
Node O1 = new Node();
O1.age = Integer.parseInt(args[1]);
O1.name = args[2];
Node O2 = new Node();
O2.age = Integer.parseInt(args[3]);
O2.name = args[4];
Node O3 = new Node();
O3.age = Integer.parseInt(args[5]);
O3.name = args[6];
System.out.println(O1 + " " + O1.age + " " + O1.name);
System.out.println(O2 + " " + O2.age + " " + O2.name);
System.out.println(O3 + " " + O3.age + " " + O3.name);
}
}
class Node{
int age;
String name;
Node next;
public Node()
{
age = 0;
name = "";
next = null;
}
}
public class Q3 {
public static void main(String[] args) {
if(args.length!=7)
{
System.out.println("Error: Usage\n Q3 ");
return;
}
Node O1 = new Node();
O1.age = Integer.parseInt(args[1]);
O1.name = args[2];
Node O2 = new Node();
O2.age = Integer.parseInt(args[3]);
O2.name = args[4];
Node O3 = new Node();
O3.age = Integer.parseInt(args[5]);
O3.name = args[6];
O1.next = O2;
O2.next = O3;
System.out.println(O1 + " " + O1.age + " " + O1.name + " " + O1.next);
System.out.println(O2 + " " + O2.age + " " + O2.name + " " + O2.next);
System.out.println(O3 + " " + O3.age + " " + O3.name + " " + O3.next);
}
}