This week you will need to submit (part of) your solutions. You MUST do your work on the lab PC NOT your own laptop!

  • Create your own repository here: https://classroom.github.com/a/y-I_t3n7
  • Open the IntelliJ IDE
  • Choose Check out from Version Control -> git in the welcome window, or choose VCS -> Check out from Version Control -> git
    • Enter URL of the Git Repo you created
    • Keep hitting “Next” or “Yes” until it opens a new window with the project you cloned.
    • Note: Always use one of the two options listed above. Do NOT try to do File->New… It won’t work correctly!
  • The full slides are available here - This includes intro and conclusion slides not shown below!

Objectives

  • To be sure we know all the things!

1. Intro


2. Mental Model: OOP and Inner Classes

Practice: Write a program to store two types of pets—cats and dogs. Your solution should use good OOP practices and demonstrate an Abstract class with at least one abstract function!

  • When you create a pet, the constructor takes a name. Cats also take a number of lives remaining.
  • All pets have a printName() function that prints the name
  • Cats have a makeNoise() function: “NAME says meow”
  • Dogs have a makeNoise() function: “woof woof says NAME”

Your main method should:

  • Create a single array with two dogs named Fido and Spot, and three cats named Fluffy, Mowzer, and Pig
  • Print the names of all the pets
  • Call the makeNoise function on the first dog and second cat
  • Use good OOP practices including abstract classes!

3. Mental Model: Threading

Practice: The q2factor package contains a class for computing the factors of large numbers and a single threaded test program that factors 1000 random numbers. Your job is to complete the ThreadedFactoring.java file so that it will use threads to split up the task of factoring the 1000 numbers.

You should not modify the Factor.java class.

You should:

  • Look at NonThreadedFactoring.java to see how to measure the time to run 1000 factor tasks using only the main thread.
  • Modify the FactorJob.java class so that it can be used by a thread to run a task. Hint: you only need to add two words.
  • Modify ThreadedFactoring.java to create and start multiple threads, each assigned the proper number of factorials to calculate.
  • Measure the time required to calculate all the factorials. Hint: remember to wait for all of the threads to finish!
  • Measure the completion time as you adjust the number of threads from 1 to 16. Fill in the table below.

Note: For the 1 thread case that thread should compute 1000 factorials, for 2 threads each should compute 500, etc. The parameter to the FactorJob class indicates how many factorials it will calculate.

This sample code might help (it can be found in the samples.thread package):


class SimpleJob implements Runnable {
 // to be used by a thread, must "implement Runnable"

 private int sleep;

 public SimpleJob(int sleep) {
  this.sleep = sleep;
 }

 // Function called when a thread with this task is started
 public void run() {
  System.out.println(Thread.currentThread().getName() + " started.");
  for (int i = 1; i <= 5; i++) {
   try {
    Thread.sleep(sleep);
    System.out.println(Thread.currentThread().getName() + " finished sleep number " + i);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}

public class ThreadSample {

 public static void main(String[] args) {

  Thread t1 = new Thread(new SimpleJob(500));
  Thread t2 = new Thread(new SimpleJob(250));
  Thread t3 = new Thread(new SimpleJob(500));

  t1.start();
  t2.start();
  t3.start();
  System.out.println("Started all threads.");

  // Wait for all threads to finish
  try {
   t1.join();
   t2.join();
   t3.join();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  System.out.println("All threads finished.");

 }
}

In this example we are using a standard class for SimpleJob, but we could have made it an inner class inside ThreadSample (the same is true for the Factor code). Using an inner class makes the most sense when you have data to be shared between the classes, which in this case we don’t.


4. Mental Model: Networking

Practice: For this problem you will create a simple text based email client that connects to the provided server program.

Modify the skeleton code in EmailClient.java so the client will follow this protocol:

  • Authenticate: send the server a String containing a username and a password separated by a colon (see the list of users and the note on sending Strings below).

  • Receive message list: the sever will then send an Integer indicating how many messages the user has in their mailbox. For each message, the server then sends two strings: first the sender of the email and then the subject of the email. Your program should read these in for each message and print the list to the screen.

  • User input: The user should then be able to enter the number for the message they would like to read into the console. The provided enterNumber() function can be used to get user input.

  • Request message: The client program should then send the requested message number as an Integer to the server. The server will respond with a String containing the message body. The client should print this to screen. Then the client should disconnect.

Hints/Tips: The starter code creates a pair of DataStreams for you to send and receive with. Use these instead of a PrintWriter/BufferedReader. The benefit of these classes is that you can send or receive a broader range of data types beyond just lines of text:

 // Send a string:
 out.writeUTF("this is a string");

 // Receive a string:
 String msg = in.readUTF();

 // Send and receive an Integer
 out.writeInt(42);
 int x = in.readInt();

The email server supports two users tim:secret and jsmith:nospam

You should NOT modify the EmailServer.java file, although you are encouraged to look at it. You will have to run the EmailServer program to test if your client works.

See the examples in samples.net for socket usage.


5. Wrap Up

Submission Instructions

Step 1: From the VCS Menu choose Commit

Step 2: Be sure ALL files are checked

Step 3: Choose Commit and Push

Step 4: Go to GitHub and verify that your code is uploaded correctly

(Only your Pets file will be graded, but you should practice to be sure everything commits correctly)


Previous - The End.

© 2003, Rahul Simha (revised 2017). Further revised by Tim Wood 2018.