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 Java

T2. Command Line Arguments in Netbeans

T3. Creating a JAR file in Netbeans

T4. Compile and run a Java program using Notepad and Console

T5. Command Line Arguments on Console

T6. Creating a JAR file on Console

T7. Github publishing



Q1. 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



Q2. Write a Java class called Node. It contains the following attributes (fields).
int age
String name

Write appropriate constructor method. Create another class called Q2 with the main method; The main method takes upto three sets of input pairs (age, name) as arguments on the command prompt.
Create three objects O1, O2 and O3 that store the information (input pairs).
Print the memory address for each object and the input pairs.
This time, export a JAR file (Q2.jar) and run the program from console.

Expected input:
java -jar Q2 21 Mohamed 22 Ahmed 19 Saleh

Expected output:
0x22445512 21 Mohamed
0x2244AC12 22 Ahmed
0x221223F1 19 Saleh



Q3. Modify the Node class from Q2.
Rename it as Q3. Add another attribute Node next in the class.
This time you will also print the address for the next attribute in each object.
Export the program as Q3.jar and run it from the console using the same input provided in Q2.

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



Q4. Look carefully at the output of program in Q3 particularly the memory addresses. Can you describe your observation?


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);

    }
}