// Jack 5 // Jack 6 // Jack 7 // Jack 8 // Jack 9

Similar documents
public synchronized void put(int i) throws InterruptedException { while (this.full) this.wait(); this.value = i; this.full = true; this.

MODULE 9 - SHEET 1. // This is programm is due to Dr P. Robinson

MODULE 3p - A Java Object

MODULE 3q - An Extended Java Object

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

Assignment 19 Deadline: Nov pm COSC211 CRN15376 Session 15 (Nov. 7)

Back To LinkedLists 1. 3 public Node left; 4 public Node right; 6 public Node(int data, Node left, Node right) {

Give one example where you might wish to use a three dimensional array

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Week 14 Lab A Linked List of Integers Maximum Points = 10

User-defined Functions. Conditional Expressions in Scheme

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JAIST) Outline of lecture

static String usersname; public static int numberofplayers; private static double velocity, time;

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

Some Sample AP Computer Science A Questions - Solutions

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Practice Midterm 2 CMPS 12A Fall 2017

CS 113 MIDTERM EXAM 2 SPRING 2013

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

1. Find the output of following java program. class MainClass { public static void main (String arg[])

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Classes - Testing your classes. Make sure your classes do what you think

CS18000: Problem Solving And Object-Oriented Programming

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

public private "foo".length() 3 t.checkexpect("foo".length(), 3)

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Chapter 6 Parallel Loops

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Variable initialization and assignment

Arrays. Eng. Mohammed Abdualal

Midterm Examination (MTA)

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

Elementary Symbol Tables

CS18000: Programming I

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

/department of mathematics and computer science 1/58

MODULE 6q - Exceptions

Question 1 [20 points]

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Quiz 1 Unit 5A Arrays/Static Name

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Selected Questions from by Nageshwara Rao

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CSC 1214: Object-Oriented Programming

CIS 265 Lecture Notes Binary Trees V. Matos DRIVER1. package csu.matos; public class Driver1 {

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

I. True/False: (2 points each)

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Distinguish between a class and an object

CS Week 14. Jim Williams, PhD

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

CSE 143 Lecture 18. More Recursive Backtracking. slides created by Marty Stepp

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

CS100M November 30, 2000

Lecture 16: Encapsulation and ADTs

CS180. Exam 1 Review

CSC 1351 The Twelve Hour Exam From Hell

Functions. Arash Rafiey. September 26, 2017

JAVA OPERATORS GENERAL

Computer Science II Exam #2, July 3, 2014 Last Name:, First Name:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Programming Fundamentals

1 Short Answer (2 Points Each)

Access and Non access Modifiers in Core Java Core Java Tutorial

Getting started with Java

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

CSC122A, Spring 2005 Computer Science II Final Exam

Introduction to Object-Oriented Programming

CHAPTER 7 OBJECTS AND CLASSES

Compiling expressions

CS 1301 Ch 8, Handout 3

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

I. True/False: (2 points each)

Lab Exercise 1. Objectives: Part 1. Introduction

I. True/False: (2 points each)

Encryption Key Search

CS 139 Practice Midterm Questions #2

Esc101 Mid Semester Exam - II

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 302 Week 9. Jim Williams

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

CS 2510 Exam 1 Fall 2010

CS111: PROGRAMMING LANGUAGE II

Transcription:

MODULE 8 - SHEET 1 public class ThreadIntro { public static void main(string[] args) { ThreadJack jack = new ThreadJack(); ThreadJill jill = new ThreadJill(); jack.start(); jill.start(); class ThreadJack extends Thread { public void run() { for (int i=0; i<10; i++) { System.out.printf("Jack %d%n", i); try { this.sleep(1000l); catch(interruptedexception e) { class ThreadJill extends Thread { public void run() { for (int i=0; i<10; i++) { System.out.printf("Jill %d%n", i); try { this.sleep(500l); catch(interruptedexception e) { This yields: Jack 0 Jill 0 Jill 1 Jack 1 Jill 2 Jill 3 Jack 2 Jill 4 Jill 5 Jack 3 Jill 6 Jill 7 Jack 4 Jill 8 Jill 9 1

Jack 5 Jack 6 Jack 7 Jack 8 Jack 9 2

MODULE 8 - SHEET 2 public class BoxConcluded { public static void main(string[] args) { Square jack = new Square(6); System.out.printf("Details of jack...%n%s%n", jack.tostring()); Square jill = new Square(5); System.out.printf("Details of jill...%n%s%n", jill); System.out.printf("Number of Squares: %d%n%n", Square.total); jack = jill; jill = null; System.gc(); System.runFinalization(); System.out.printf("Number of Squares: %d%n%n", Square.total); System.out.printf("Details of jack...%n%s%n", jack); class Square { public static int total = 0; record the number of Squares... private int side; public Square(int e) { this.side = e; this.total++;...by incrementing total. public int area() { return this.side*this.side; public String tostring() { return "Square: Side = " + this.side + "\n" + " Area = " + this.area() + "\n"; public void finalize() { this.total--; This yields: Details of jack... Square: Side = 6 Area = 36 Details of jill... Square: Side = 5 Area = 25 Number of Squares: 2 3

Number of Squares: 1 Details of jack... Square: Side = 5 Area = 25 4

MODULE 8 - SHEET 3 public class ReverseList { public static void main(string[] args) { Link start = new Link(16); start.put(8); start.put(4); start.put(64); start.put(32); System.out.printf("List elements: %s%n", start); System.out.printf("Element sum: %d%n",start.sum()); Link rev = start.reverse(); System.out.printf("List elements: %s%n", rev); System.out.printf("Element sum: %d%n", rev.sum()); class Link { private int val; private Link next; public Link(int n) { this.val = n; this.next = null; public void put(int k) { if (this.next == null) this.next = new Link(k); this.next.put(k); public Link reverse() { if (this.next == null) return this; { Link temp = this.next.reverse(); temp.put(this.val); return temp; public String tostring() { return this.val + (this.next == null? "" : " " + this.next.tostring()); public int sum() { return this.val + (this.next == null? 0 : this.next.sum()); 5

MODULE 8 - SHEET 4 public class TreeSort { public static void main(string[] args) { Node tree = new Node(16); tree.put(8); tree.put(4); tree.put(64); tree.put(32); System.out.printf("Tree elements: %s%n", tree); System.out.printf("Element sum: %d%n", tree.sum()); class Node { private Node left; private int val; private Node right; public Node(int n) { this.left = null; this.val = n; this.right = null; public void put(int k) { if (k < this.val) if (this.left == null) this.left = new Node(k); this.left.put(k); if (this.right == null) this.right = new Node(k); this.right.put(k); public String tostring() { String s = ((this.left!= null)? this.left.tostring() : "") + this.val + " " + ((this.right!= null)? this.right.tostring() : ""); return s; public int sum() { int s = ((this.left!= null)? this.left.sum() : 0) + this.val + ((this.right!= null)? this.right.sum() : 0); return s; 6

MODULE 8 - SHEET 5 public class CroquetA { private static int count=0; private static int[] plan = new int[28]; private static boolean[] alreadyplayed = new boolean[193]; private static final int p1 = 1, p2 = 2, p3 = 4, p4 = 8, p5 =16, p6 =32, p7 =64, p8 =128; private static final int maxgame = 28; public static void main(string[] args) { for (int i=0; i<=192; i++) alreadyplayed[i] = false; tryit(0); System.out.printf("There are %d solutions%n", count); private static void tryit(int game) { if (game == maxgame) { count++; printout(); System.exit(0); Abort after printing first solution return; int imposs = 0; for (int i = game & 0x1C; i<game; i++) imposs = plan[i]; int poss = ~imposs & 0xFF; int[] player = new int[8]; int k = -1; while (poss!=0) { player[++k] = poss & -poss; poss &= ~player[k]; for (int i=0; i<k; i++) for (int j=i+1; j<=k; j++) { int pi = player[i]; int pj = player[j]; int pair = pi pj; if (!(alreadyplayed[pair])) { plan[game] = pair; alreadyplayed[pair] = true; tryit(game+1); alreadyplayed[pair] = false; private static void printout() { for (int lawn=0; lawn<4; lawn++) { for (int game=lawn; game<maxgame; game += 4) System.out.printf("%s ", match(plan[game])); System.out.printf("%n"); 7

private static String match(int pair) { String s = ""; for (int p = pair & -pair; pair!= 0; p = pair & -pair) { switch(p) { case p1: s += 1; break; case p2: s += 2; break; case p3: s += 3; break; case p4: s += 4; break; case p5: s += 5; break; case p6: s += 6; break; case p7: s += 7; break; case p8: s += 8; pair &= ~p; return s; 8