Midterm 2 Practice CIS 113 Spring 2005. public class Card { public int rank; public String suit; Card(){rank = -1; suit = "-";} Card(Card card){rank =

Slides:



Advertisements
Similar presentations
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
Advertisements

C9, a case study: Solitaire
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
AP Statistics Section 6.2C Independent Events & The Multiplication Rule.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CSCI 160 Midterm Review Rasanjalee DM.
1 Various Methods of Populating Arrays Randomly generated integers.
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Probability theory and average-case complexity. Review of probability theory.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Mock test review Revision of Activity Diagrams for Loops,
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
CSCE 2100: Computing Foundations 1 Probability Theory Tamara Schneider Summer 2013.
Introduction to Arrays ISM 615 Dr. Hamid Nemati Summer 2001.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
TIMES 3 Technological Integrations in Mathematical Environments and Studies Jacksonville State University 2010.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
This week in CS 5 HW 9 (2 problems) M/T sections W/Th sections due Sunday, 11/4 at midnight due Monday, 11/5 at midnight Recitation for HW9 -- Friday 11/2.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Comparison-Based Sorting & Analysis Smt Genap
Chapter 4: Control Structures II
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
SEEM Java – Basic Introduction, Classes and Objects.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Probability Bingo October 3, D Mathematics.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Java linked list.
int [] scores = new int [10];
Data Structures Arrays and Lists Part 2 More List Operations.
Probability theory and average-case complexity. Review of probability theory.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Midterm preview.
Values vs. References Lecture 13.
Single Dimensional Arrays
Section 12.2 Probability.
Chapter 6 More Conditionals and Loops
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
Namespaces, Scopes, Access privileges
Truth tables: Ways to organize results of Boolean expressions.
CIS 110: Introduction to Computer Programming
Building Java Programs
First Semester Review.
Stacks, Queues, ListNodes
Presentation transcript:

Midterm 2 Practice CIS 113 Spring 2005

public class Card { public int rank; public String suit; Card(){rank = -1; suit = "-";} Card(Card card){rank = card.rank; suit = card.suit;} void show(){System.out.print(rank + suit);} } A card game called Twenty uses a special deck with 20 cards. The deck has two suits - Red and Black. In each suit there are cards with rank 0 through 9. The definition of Card is below. Write the member declarations and a constructor for the class CardDeck to create a deck of cards for Twenty. You may use “R” for “Red” and “B” for “Black”. Hint: use an array of type Card and of size 20. Problem 1

public class CardDeck { final static int DECK_SIZE = 20; Card[] deck = new Card[DECK_SIZE]; CardDeck(){ for (int i = 0; i < DECK_SIZE; ++i){ deck[i] = new Card(); } for (int r = 0; r < DECK_SIZE/2; ++r){ (deck[r]).rank = r; deck[r + DECK_SIZE/2].rank = r; deck[r].suit = "B"; deck[r + DECK_SIZE/2].suit = "R"; } } } Solution

Key points in the solution Card[] deck says deck will be an array of Cards new Card[20] allocates memory for the array of Cards The for loop enclosing deck[i] = new Card(); creates 20 Cards and puts pointers to them into deck. Very important! Until this loop is executed deck is filled with null pointers! The remaining loops give the Card s the proper ranks and and suits

Assume that the class Card and the constructor for CardDeck are given. Write a method for the class CardDeck with the following signature: void shuffle(int times) Each ‘time’ in the shuffle is accomplished by choosing two cards in the 20-Card deck at random and swapping them. Next Problem (2)

void shuffle(int times){ for (int i = 0; i < times; ++i){ // select the two cards to be swapped int a = (int)(Math.random() * DECK_SIZE); int b = (int)(Math.random() * DECK_SIZE); // save the value of the first card Card temp = deck[a]; // do the swap deck[a] = deck[b]; deck[b] = temp; } Question: What happens if you don’t create a new Card to save the value of deck[a]? Solution

Next Problem (3) Write a method for CardDeck with the following signature: void show() show should display the cards, 10 to a line, with a space between cards. A sample output is given below. Hint: there is a method show() in the class Card. 3R 2B 4R 1R 9B 9R 1B 6B 7R 2R 4B 6R 8B 5B 3B 7B 8R 0B 0R 5R

Solution void show(){ // print out 10 cards on the first line for (int i = 0; i < DECK_SIZE/2; ++i){ deck[i].show(); System.out.print(" "); } System.out.println(); // print out 10 cards on the second line for (int i = DECK_SIZE/2; i < DECK_SIZE; ++i){ deck[i].show(); System.out.print(" "); } System.out.println(); }

Next Problem (4) Write a class CardStuff using the methods of the class CardDeck. CardStuff should do the following: - create a deck of cards for the game of Twenty - display the deck of cards - shuffle the cards 100 times - display the shuffled deck

Solution public class CardStuff { public static void main(String[] args) { CardDeck deckA = new CardDeck(); deckA.show(); System.out.println(); deckA.shuffle(100); deckA.show(); }

Next Problem (5) Write a method with the following signature: public static boolean AtLeastHalfEven(int[] intArr) This method should return true if and only if at least half of the int’s in the intArr are even. You may assume that intArr is fully populated, that is, there are no nulls in it.

Solution public static boolean AtLeastHalfEven(int[] intArr){ int evenCount = 0; for (int i = 0; i < intArr.length; ++i){ if (intArr[i]%2 == 0){ evenCount++; } if (evenCount >= intArr.length/2){ return true; } else { return false; }

Next Problem (6) Write a class named CheckHalfEven. This class should have a main method that does the following: - creates an array of 20 random int’s in the range calls AtLeastHalfEven to determine if half or more of the numbers in the array are even - prints out the result

Solution public static void main(String[] args) { final int MAX_NUM = 20; int[] iArr = new int[MAX_NUM]; for (int i = 0; i < MAX_NUM; ++i){ iArr[i] = (int)(Math.random()*100); } if (AtLeastHalfEven(iArr)){ System.out.println("“At least half even"); } else { System.out.println(“Less than half even"); }

Next Problem (7) Write a method with the following signature: static int AboveThreshold(int[] arr, int threshold) This method should: -determine how many int ’s in arr[] strictly greater than threshold - return that number You may assume that all the elements of arr[] are int ’s.

Next Problem (8) Write a class AboveN with a main method. This method should: -create a array of 100 random int’s in the range call AboveThreshold to determine how many of them are greater than 45 - print this result to the standard output

Next Problem(9) It is a fact of geometry that there are only five regular solids. A regular solid is one in which all angles are the same and all faces are the same. For example, a cube is a regular solid. The number of faces on the five regular solids are 4, 6, 8, 12 and 20. The regular (Platonic) solids make good dice because each side has a equal probability of coming up. Write a class PlatonicDie. The class should include two methods with signatures: Die(int sides) int roll() If a die has 4, 6, 8, 12 or 20 sides, roll() should return a random number from 1 to the number of sides. Otherwise, roll() should return –1. (Hint: modify the Die class in the homework.)