Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Similar documents
AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Supplementary Test 1

CS141 Programming Assignment #6

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

2.8. Decision Making: Equality and Relational Operators

download instant at

Control Statements: Part 1

Introduction to Java Applications

Introduction to Java Applications

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Repetition, Looping. While Loop

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Assignment 2.4: Loops

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

AP CS Unit 3: Control Structures Notes

1 Short Answer (10 Points Each)

Introduction to Computer Science Unit 3. Programs

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

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Java Coding 3. Over & over again!

Example: Monte Carlo Simulation 1

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Course Outline. Introduction to java

ITERATION WEEK 4: EXMAPLES IN CLASS

CS 101 Spring 2007 Midterm 2 Name: ID:

Top-Down Program Development

COMP 110 Programming Exercise: Simulation of the Game of Craps

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

What did we talk about last time? Examples switch statements

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Object Oriented Programming. Java-Lecture 6 - Arrays

Last Class. While loops Infinite loops Loop counters Iterations

Claremont McKenna College Computer Science

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Fundamentals of Programming Data Types & Methods

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

1 Short Answer (15 Points Each)

CSCI 355 Lab #2 Spring 2007

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

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

Software Practice 1 Basic Grammar

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

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

Introduction to the Java Basics: Control Flow Statements

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

AP Computer Science Unit 1. Programs

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

AP COMPUTER SCIENCE A

Introduction to Java Unit 1. Using BlueJ to Write Programs

The keyword list thus far: The Random class. Generating "Random" Numbers. Topic 16

Midterm Examination (MTA)

CS 101 Fall 2006 Midterm 3 Name: ID:

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

CSCI 355 LAB #2 Spring 2004

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

Topic 16. battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." -Alfred North Whitehead

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Loops. CSE 114, Computer Science 1 Stony Brook University

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

STUDENT LESSON A12 Iterations

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Topic 12 more if/else, cumulative algorithms, printf

Final Exam. COMP Summer I June 26, points

Programming with Java

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

AP Computer Science Homework Set 1 Fundamentals

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

RANDOM NUMBER GAME PROJECT

COMP-202 Unit 4: Programming with Iterations

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Full file at

Static Methods. Why use methods?

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Over and Over Again GEEN163

Summer Assignment for the School Year

Exercise (Revisited)

Handout 4 Conditionals. Boolean Expressions.

Section 2.2 Your First Program in Java: Printing a Line of Text

CS 200 Using Objects. Jim Williams, PhD

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CS 177 Recitation. Week 1 Intro to Java

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Object Oriented Programming. Java-Lecture 1

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Chapter 3. Selections

Arrays. Eng. Mohammed Abdualal

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Transcription:

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM Let s get some practice creating programs that repeat commands inside of a loop in order to accomplish a particular task. You may want to create these inside a main method of a class, one right after the next. For example, public class LoopPractice { public static void main(string[] args) { //Loop 1 System.out.println( ------------------Loop 1---------------------- int i = 1; while( ) { } //Loop 2 System.out.println( ------------------Loop 2---------------------- i = 1; }//end main } //end class NOTE: During the this project, you are going to define 3 different methods; 1 Page

1. the main method: Will load up 5 sound files from disk into RAM, call the other two methods in the beginning and then run through several example loops. 2. dispauthor: this method will display some information about who wrote the program, what the title of the program is and what period and date this program was generated. 3. playsound: this method will choose from among 5 loaded audio clips and play one of them randomly when the program starts up. For this, there will need to be 5 global variables to hold the audio clips. 1. Make the following numbers print to the screen 1 4 7 10 13 16 2. Make the following numbers print to the screen 20 15 10 5 3. Print out 500 asterisks with no spaces or newlines think System.out.print (make sure to use a blank System.out.println() when this is done so your next loop will start on a new line) 4. Ask the user to enter in a number. (You will need to create an instance of the Scanner class to read the System.in stream(keyboard)) Print out that many * s on the screen without spaces or newlines. You may use your Scanner object in numbers 5, 6 and 9 also!! Please enter a number 7 ******* HINT: This is a lot like the last loop only the loop invariant is now a variable 2 Page

5. Print all the odd numbers from 1 to a user specifiable upper limit (inclusive) Please enter a number 9 1 3 5 7 9 HINT: The number 9 should be included!!! 6. It s about now that most students are getting agitated with running through each of the loops in order to test the later loops. Make a menu that the user can choose from and allow them to select which loop practice option to run. The following loop will run forever until the user enters an exit option int choice; while(true) { System.out.println( Please enter in a choice, 0 to quit ); choice = input.nextint(); //assumes a Scanner object has been created if (choice == 0) { break; //exits the loop } }//end loop 7. Write a program that reads in numbers from the keyboard and then when the user enters a -1, gives the average of the numbers. Allow for floating point values. Round off the answer to the nearest hundredth. See cookbook for rounding. Below is shown an example: Input a number, -1 to find average: 20 Sum so far = 20, # s so far = 1 Input a number, -1 to find average: 25 Sum so far = 45, # s so far = 2 Input a number, -1 to find average: 4 Sum so far = 49, # s so far = 3 Input a number, -1 to find average: -1 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 3 Page

Your average is 16.33 for the 3 numbers entered &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 8. Write a method called dispauthor(). You may define this above or below the main but NOT inside the main. The method should display your name, period you have java and the date and title of this project inside some sort of box. Call your method from the beginning of your main. Remember, to call a non-static method that has 0 inputs and 0 outputs, use the following approach; dispauthor(); 9. Write a method called playsound() that plays a random sound from among 5 sounds that you have loaded up in the beginning of the program (see the cookbook for loading up sounds, use.wav,.midi or.au formats) Call your method after you call dispauthor from the main. You may define this above or below the other 2 methods but NOT inside. Remember, to call a non-static method that has 0 inputs and 0 outputs, use the following approach; playsound(); NOTE: You MUST successfully load the 5 audio clips from disk into RAM before they can be played. To do this, we must declare 5 global AudioClips above the main but inside the class. 10. Probability: Rolling 6 sided dice can teach us about probability. Allow the user to choose a number of rolls for 2 six sided dice. Add the dice together and keep track of how many of each of the sums are rolled. Print out a frequency distribution table and then a frequency histogram. How many pair to roll->15 Rolling Roll Sum Frequency ---------- ------------ 2 1 3 0 4 1 5 2 6 2 7 4 8 1 4 Page

9 2 10 1 11 1 12 0 Frequency Histogram ----------------------- 2: # 3: 4: # 5: ## 6: # 7: #### 8: # 9: ## 10: # 11: # 12: Bonus # 1: Pyramid Using a combination of loops, ask the user what size pyramid they want to display and then print a scalable pyramid. What size pyramid? 4 * *** ***** ******* What size pyramid? 5 * *** ***** ******* ********* What size pyramid? 2 * *** Bonus # 2: Multiplication Table Using nested loops, print out a multiplication table for the numbers 1-? 5 Page

To help out, try printing out the first two lines of the heading as follows; System.out.println( X\t \t1\t2\t3\t4\t5\t6\t7\t8\t9 );//etc System.out.println( ------------------------------------------------- ); Then start your loops int r=?. c=? while( ) MUST ASK THE USER WHAT THE HIGHEST NUMBER FOR THE TABLE HEADINGS IS From 2->20 X 1 2 3 4 5 6 7 8 9 ------------------------------------------------------------------------------ 1 1 2 3 4 5 6 7 8 9 2 2 4 6 8 10 12 14 16 18 3 3 6 9 12 15 18 21 24 27 4 4 8 12 16 20 24 28 32 36 5 5 10 15 20 25 30 35 40 45 6 6 12 18 24 30 36 42 48 54 7 7 14 21 28 35 42 49 56 63 8 8 16 24 32 40 48 56 64 72 9 9 18 27 36 45 54 63 72 81 Project Name App Name Assign 3 Loop Practice LoopPractice Rubric Loops 1-5 75 Menu for Part 6 15 Loop 7 30 Method 7 15 Method 9 15 Problem 10 40 Comments 10 TOTAL 200 Rubric for Bonus Scalable Pyramid 15 Multiplication Table with nested 8 loops 6 Page