Unit7-Module0.docx

0.1 Exercise

Program Code:

// List node.

class ListItem{

public int data = 0;

ListItem next = null;

}// End of class ListItem.

// A separate list class.

class MyLinkedList{

// Instance variables.

ListItem front = null;

ListItem rear = null;

// Instance method to add a data item.

public void addData(int d){

if(front == null){

front = new ListItem();

front.data = d;

rear = front;

rear.next = null;

}else{

rear.next = new ListItem();

rear.next.data = d;

rear.next.next = null;

rear = rear.next;

}

}// End of method addData.

// Instance method to print the list.

public void printList(){

ListItem listPtr = front;

int i = 1;

while(listPtr != null){

System.out.println("Item # " + i + ": " + listPtr.data);

i++;

listPtr = listPtr.next;

}

}// End of method printList.

}// End of class MyLinkedList.

// Here is the main class.

class TestList2{

public static void main(String[] argv){

// Create a new list object.

MyLinkedList L = new MyLinkedList();

// Insert number 1 to 10.

for(int i = 1; i <= 10; i++){

L.addData(i); // Invoking the addData method.

}

// Print out the contents of lis L.

L.printList(); // Invoking the printList method.

// Creating another list:

MyLinkedList L2 = new MyLinkedList();

// Insert numbers from 100 to 1000 in steps of 100.

for(int j = 100; j <= 1000; j += 100){

L2.addData(j);

}

// Print out the contents of L2.

L2.printList();

}// End of public class.

}// End of main class.

Memory Picture After Second Iteration of the “for” Loop in main.

Computer Memory

0

1

2

3

1000

1001

2002

1

1002

1003

2000

2001

2002

null

2

2003

0.2 Exercise

TestListMember.java – Implemented with the “memberOf” method

class ListItem {

// INSERT YOUR CODE HERE

int data = 0;

ListItem next = null;

// Constructor.

public ListItem(int d){

data = d;

next = null;

}

// Accessor.

public int getData(){

return data;

}

}// End of ListItem class.

class MyLinkedList{

// INSERT YOUR CODE HERE

ListItem front = null;

ListItem rear = null;

int numItems = 0; // Current number of items.

// Instance method to add a data item.

public void addData(int d){

if(front == null){

front = new ListItem(d);

rear = front;

}else{

rear.next = new ListItem(d);

rear = rear.next;

}

numItems++;

}

// Instance method print list contents

public void printList(){

ListItem listPtr = front;

System.out.println("List: (" + numItems + " items)");

int i = 1;

while(listPtr != null){

System.out.println("Item# " + i + ": " + listPtr.getData());

i++;

listPtr = listPtr.next;

}

}

// The memberOf method By: Mariana

public boolean memberOf(int content){

ListItem listPtr1 = front;

boolean itemIsIn = false;

int j = 1;

while(listPtr1 != null){

if(listPtr1.getData() == content){

itemIsIn = true;

break;

}

j++;

listPtr1 = listPtr1.next;

}

return itemIsIn;

}

}// End of MyLinkedList class

// Test class.

class TestListMember {

public static void main (String[] argv)

{

// Create a new list object.

MyLinkedList L = new MyLinkedList ();

// Let's add 10 random numbers between 1 and 100

java.util.Random rand = new java.util.Random ();

for (int i=1; i <= 10; i++) {

// int k = RandTool.uniform (1,100);

int k = rand.nextInt (100);

L.addData (k);

}

// Print the contents.

L.printList();

// We will test each number in [1,100]

for (int i=1; i<=100; i++) {

if (L.memberOf (i)) {

System.out.println ("Value " + i + " is in the list");

}

}

}

}

Memory Picture After Second Iteration of the first “for” Loop in main.

Computer Memory

0

1

2

3

1000

1001

2002

13

1002

1003

2000

2001

2002

null

42

2003

0.3 Exercise

A generic Linked list using object

/***

***/

class ListItem{

Object data = null;

ListItem next = null;

// Constructor.

public ListItem(Object obj){

data = obj;

next = null;

}

// Accessor.

public Object getData(){

return data;

}

}// End of ListItem class.

class MyLinkedList{

ListItem front = null;

ListItem rear = null;

int numItems = 0;

// Instance method to add a data item.

public void addData(Object obj){

if(front == null){

front = new ListItem(obj);

rear = front;

}else{

rear.next = new ListItem(obj);

rear = rear.next;

}

numItems++;

}

// Instance method to print out data.

public void printList(){

ListItem listPtr = front;

System.out.println("List: (" + numItems + " items)");

int i = 1;

while(listPtr != null){

System.out.println("Item# " + i + ": " + listPtr.getData());

i++;

listPtr = listPtr.next;

}

}

}// End of MyLinkedList class.

// An object to use in the list.

class Person{

String name;

String ssn;

// Constructor.

public Person(String name, String ssn){

this.name = name;

this.ssn = ssn;

}

public String toString(){

System.out.println("In toString() of Person: name = " + name);

return "Person: name = " + name + ", ssn = " +ssn;

}

}// End class Person.

// Test class.

public class TestList4{

public static void main(String[] argv){

// Create a new list object.

MyLinkedList L = new MyLinkedList();

// Create a Person instance and add it to list.

Person p = new Person("Terminator", "444-43-4343");

L.addData(p);

L.addData(new Person ("Rambo", "555-54-5454"));

L.addData(new Person ("James Bond", "666-65-6565"));

L.addData(new Person ("Bruce Lee", "777-76-7676"));

L.printList();

}

}// End of class "TestList4".

0.4 Exercise

Memory picture after the second Person instance is inserted into the list

Computer Memory

1000

1001

444-43-4343

Terminator

1002

1003

555-54-5454

Rambo

2000

1001

2002

2001

2002

1003

null

2003

0.5 Exercise

“memberOf” Method

// A node that contains any "Object"

class ListItem {

Object data = null;

ListItem next = null;

// Constructor.

public ListItem(Object obj){

data = obj;

next = null;

}

// Accessor.

public Object getData(){

return data;

}

}// End of class ListItem.

class MyLinkedList {

// INSERT YOUR CODE HERE

ListItem front = null;

ListItem rear = null;

int numItems = 0;

// Instance method to add a data item.

public void addData(Object obj){

if(front == null){

front = new ListItem(obj);

rear = front;

}else{

rear.next = new ListItem(obj);

rear = rear.next;

}

numItems++;

}

// Instance method to print out data.

public void printList(){

ListItem listPtr = front;

System.out.println("List: (" + numItems + " items)");

int i = 1;

while(listPtr != null){

System.out.println("Item# " + i + ": " + listPtr.getData());

i++;

listPtr = listPtr.next;

}

}

// Instance method to check for data in the list.

public boolean memberOf(Object obj){

ListItem listPtr = front;

boolean objectIsIn = false;

int i = 1;

while(listPtr != null){

if(equals(listPtr.getData()) == equals(obj)){

objectIsIn = true;

break;

}

i++;

listPtr = listPtr.next;

}

return objectIsIn;

}

}// End of class MyLinkedList.

// An object to use in the list:

class Person {

String name;

String ssn;

// Constructor.

public Person (String name, String ssn)

{

this.name = name; this.ssn = ssn;

}

// Override toString()

public String toString ()

{

return "Person: name=" + name + ", ssn=" + ssn;

}

// INSERT YOUR CODE for equals() here

public String equals(){

return name;

}

}

// Test class.

public class TestListMember2 {

public static void main (String[] argv)

{

// Create a new list object.

MyLinkedList L = new MyLinkedList ();

L.addData (new Person ("Terminator", "444-43-4343"));

L.addData (new Person ("Rambo", "555-54-5454"));

L.addData (new Person ("James Bond", "666-65-6565"));

L.addData (new Person ("Bruce Lee", "777-76-7676"));

L.printList();

// Test membership.

Person p = new Person ("Rambo", "555-54-5454");

if (L.memberOf (p))

System.out.println ("Rambo is in the list");

else

System.out.println ("Rambo is not in the list");

Person p2 = new Person ("PeeWee", "555-54-5454");

if (L.memberOf (p2))

System.out.println ("PeeWee is in the list");

else

System.out.println ("PeeWee is not in the list");

}

}

0.6 Exercise

0.7 Exercise

Enumeration

class ListItem {

Object data = null; // The universal Object.

ListItem next = null;

// Constructor.

public ListItem (Object obj)

{

data = obj; next = null;

}

// Accessor.

public Object getData ()

{

return data;

}

}

class MyLinkedList {

ListItem front = null;

ListItem rear = null;

int numItems = 0;

public void addData (Object obj)

{

if (front == null) {

front = new ListItem (obj);

rear = front;

}

else {

rear.next = new ListItem (obj);

rear = rear.next;

}

numItems++;

}

public void printList ()

{

ListItem listPtr = front;

System.out.println ("List: (" + numItems + " items)");

int i = 1;

while (listPtr != null) {

System.out.println ("Item# " + i + ": "

+ listPtr.getData());

// Must implement toString()

i++;

listPtr = listPtr.next;

}

}

ListItem enumPtr = null;

// INSERT THE ENUMERATION METHODS HERE

}

class Person {

String name;

String ssn;

// Constructor.

public Person (String name, String ssn)

{

this.name = name; this.ssn = ssn;

}

// Override toString()

public String toString ()

{

return "Person: name=" + name + ", ssn=" + ssn;

}

}

// Test class.

class TestList5 {

public static void main (String[] argv)

{

// Create a new list object.

MyLinkedList L = new MyLinkedList ();

// Create a Person instance and add it to list.

Person p = new Person ("Terminator", "444-43-4343");

L.addData (p); // p is a subclass of Object and so

// p is automatically cast to Object

// We don't really need a Person variable:

L.addData (new Person ("Rambo", "555-54-5454"));

L.addData (new Person ("James Bond", "666-65-6565"));

L.addData (new Person ("Bruce Lee", "777-76-7676"));

// Print contents.

L.printList();

// Enumerate items.

L.startEnumeration();

while (L.hasMoreElements()) {

Person p2 = (Person) L.getNextElement();

System.out.println (p2);

}

}

}

Memory Pictures

0.8 Exercise

Java Generics

Does the program compile?

Yes, it successfully compiles to form ListProblem.class from ListProblem.java

What kind of problem do you see and where does the problem occur?

The problem occurs in the “main” class, specifically under the while loop where the program is implemented to enumerate through the list.

The problem is an Exception in thread “main” java.lang.ClassCastException, described as java.lang.String cannot be cast to java.lang.Integer.

This exception indicates that the application’s code has attempted to cast a specific object to a class of which it is not an instance. In this case, the Integer object cannot be casted to a String object.

0.9 Exercise

StackExample.java

import java.util.*;

public class StackExample{

public static void main (String[] argv){

// Create an Array toDoList

Deque<String> toDoList = new ArrayDeque<>();

toDoList.push("Homework");

toDoList.push("Laundry");

toDoList.push("Hang out");

toDoList.push("Binge watch");

System.out.println(toDoList);

}

}

0.10 Exercise

Output of TestStack.java:

Person: name=Jean Grey, ssn=888-89-8989

Person: name=Black Widow, ssn=333-34-3434

Person: name=Black Widow, ssn=333-34-3434

0.11 Exercise

Output of TestHashMap.java:

Dyspeptic Dave

3

0.12 Exercise

Output of TestPersonList.java:

false

0.13 Exercise

Are the pointers to the two ‘Gita’ instances the same?

No, I have observed that the addresses are different

0.14 Exercise

Add “equlals()” method in TestPersonList.java

import java.util.*;

class Person {

String name;

String nickName;

public Person (String name, String nickName)

{

this.name = name; this.nickName = nickName;

}

public boolean equals (Object obj)

{

Person p = (Person) obj;

// WRITE the equality logic here:

if (this.name.equals(p.name) && this.nickName.equals(p.nickName)){

return true;

}

return false;

}

}

public class TestPersonList2 {

public static void main (String[] argv)

{

LinkedList<Person> list = new LinkedList<>();

Person p = new Person ("Franco", "Flatulent Franco");

list.add (p);

p = new Person ("Gita", "Gluttonous Gita");

list.add (p);

System.out.println (p); // Pointer

p = new Person ("Heinrich", "Hemorrhaging Heinrich");

list.add (p);

p = new Person ("Ida", "Insidious Ida");

System.out.println (list.contains(p)); // Should print false.

char[] name = {'G','i','t','a'};

p = new Person (new String(name), "Gluttonous Gita");

System.out.println (list.contains(p)); // Should print true.

System.out.println (p); // Pointer

}

}

0.15 Exercise

0.16 Exercise

Implementation of TestProperties.java

import java.util.*;

import java.io.*;

public class TestProperties{

public static void main(String[] argv){

// Create a new Properties object

Properties p = new Properties();

// Insert some properties

p.put("Circle1.center.x", "100");

p.put("Circle1.center.y", "250");

p.put("Circle1.radius", "30");

// Note: both the property name and value are String's.

p.put("Circle2.center.x", "60");

p.put("Circle2.center.y", "90");

p.put("Circle2.radius", "10");

p.put("Square1.topleft.x", "300");

p.put("Square1.topleft.y", "400");

p.put("Square1.side", "40");

// Print all the properties to the screen

p.save(System.out, "Geometric objects");

// Write the properties to text file called "geodata"

try{

// File-related IO requires try-catch

FileOutputStream f = new FileOutputStream("geodata");

p.save(f, "Geometric objects");

}catch(IOException e){

System.out.println("Cannot open file");

System.exit(0);

}

}// "main"

}

0.17 Exercise

Implementation of TestProperties2.java

import java.util.*;

import java.io.*;

public class TestProperties{

public static void main(String[] argv){

// Create a new Properties object

Properties p = new Properties();

// Insert some properties

p.put("Circle1.center.x", "100");

p.put("Circle1.center.y", "250");

p.put("Circle1.radius", "30");

// Note: both the property name and value are String's.

p.put("Circle2.center.x", "60");

p.put("Circle2.center.y", "90");

p.put("Circle2.radius", "10");

p.put("Square1.topleft.x", "300");

p.put("Square1.topleft.y", "400");

p.put("Square1.side", "40");

// Print all the properties to the screen

p.save(System.out, "Geometric objects");

// Write the properties to text file called "geodata"

try{

// File-related IO requires try-catch

FileOutputStream f = new FileOutputStream("geodata");

p.save(f, "Geometric objects");

}catch(IOException e){

System.out.println("Cannot open file");

System.exit(0);

}

}// "main"

}

0.18 Exercise

Implementation of TestProperties.java

import java.io.*;

import java.util.*;

public class TestProperties3{

public static void main(String[] argv){

Properties p = new Properties();

// Read properties from the file "geodata".

try{

FileInputStream f = new FileInputStream("geodata");

p.load(f);

}catch(IOException e){

System.out.println("Cannot open file");

System.exit(0);

}

// The entire list of property NAMES can be enumerated

System.out.println("Property Names & Values");

Enumeration e = p.propertyNames();

while(e.hasMoreElements()){

String propName = (String) e.nextElement();

String propValue = p.getProperty(propName);

System.out.println(propName + ": " + propValue);

}

}

}

0.19 Exercise

SystemProperties.java

What does it print out?

It prints out details of the java software installed on the system. The details included version, vendor, installation path, user, and libraries among others.

Is the method getProperties() in System an instance or class (static) method?

It is an instance method because when I run a Java program, I actually start a JVM instance and that instance will have its own properties.

2