CSIS 10A Assignment 4 SOLUTIONS

Similar documents
CSIS 10A Assignment 3 Due: 2/21 at midnight

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Assignment 8B SOLUTIONS

Chapter 4: Loops and Files

Chapter 4: Loops and Files

Midterm Examination (MTA)

Task #1 The if Statement, Comparing Strings, and Flags

Chapter 5 Lab Methods

Chapter 5 Lab Methods

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

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.

Chapter 3 Lab Decision Structures

1 Short Answer (10 Points Each)

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

! 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

Object-Oriented Programming in Java

Input. Scanner keyboard = new Scanner(System.in); String name;

Chapter 4 Lab. Loops and Files. Objectives. Introduction

CSIS 10A Assignment 9 Solutions

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

ITERATION WEEK 4: EXMAPLES IN CLASS

Flow of Control: Loops. Chapter 4

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

A+ Computer Science -

Lesson 7 Part 2 Flags

Problem Solving With Loops

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Lecture 8 " INPUT " Instructor: Craig Duckett

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

What two elements are usually present for calculating a total of a series of numbers?

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Oct Decision Structures cont d

Chapter 1 Lab Algorithms, Errors, and Testing

2.2 - Making Decisions

St. Edmund Preparatory High School Brooklyn, NY

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

Loops. CSE 114, Computer Science 1 Stony Brook University

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

A token is a sequence of characters not including any whitespace.

Over and Over Again GEEN163

Name: Checked: Preparation: Response time experiment accessing the system clock

Lab 6. Name: Checked:

Name: Checked: Preparation: Response time experiment accessing the system clock

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

1 Short Answer (5 Points Each)

Chapter 7: Arrays and the ArrayList Class

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

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

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

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

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

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

1 Short Answer (2 Points Each)

CS141 Programming Assignment #6

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CSIS 10A Assignment 4 SOLUTIONS

When we reach the line "z = x / y" the program crashes with the message:

STUDENT LESSON A7 Simple I/O

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Programming with Java

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously.

Last Class. While loops Infinite loops Loop counters Iterations

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

CAT.woa/wa/assignments/eclipse

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Building Java Programs

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Anatomy of a Java Program: Comments

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment

What methods does the String class provide for ignoring case sensitive situations?

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Top-Down Program Development

Repetition, Looping. While Loop

File I/O and Exceptions

for Diane Christie University of Wisconsin Stout

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

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

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Lecture 10. Instructor: Craig Duckett

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Building Java Programs

Question: Total Points: Score:

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Data Types and the while Statement

Basic Computation. Chapter 2

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

CS 201, Fall 2016 Sep 28th Exam 1

CS Computers & Programming I Review_01 Dr. H. Assadipour

Transcription:

CSIS 10A Assignment 4 SOLUTIONS Read: Chapter 4 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be completed in Exercises file Find the Error: 1. // This code contains ERRORS! // It adds two numbers entered by the user. int num1, num2; String input; char again; XXXXX should be char again = 'Y' so the while loop will be true Scanner keyboard = new Scanner(System.in); while (again == 'y' again == 'Y') XXXX needs a here System.out.print("Enter a number: "); num1 = keyboard.nextint(); System.out.print("Enter another number: "; num2 = keyboard.nextint(); System.out.println("Their sum is "+ (num1 + num2)); System.out.println("Do you want to do this again? "); keyboard.nextline(); // Consume remaining newline input = keyboard.nextline(); again = input.charat(0); XXXX needs a here

2. // This code contains ERRORS! int count = 1, total; XXXX total needs to be initialized to 0 while (count <= 100) XXXX needs a here total += count; XXXX needs count++; here XXXX needs a here System.out.print("The sum of the numbers 1-100 is "); System.out.println(total); Algorithm Workbench 1. Write a while loop that displays the following set of numbers: 20, 15, 10, 5, 0 int k = 20; while (k>=0) System.out.println(k + ", "); k -=5; // or, k = k 5; System.out.println(k ); // display last number without "," after it for (k = 0; k<=1000; k+=10) System.out.println(k + ", "); System.out.println(k ); // display last number without "," after it (should be 93.84) int a = 1, b = 30; double sum = 0.0; while ( a <=30 ) sum += (double)a/b; a++; b--;

for (count = 0; count < 50; count++) System.out.println("count is " + count); Part 2: Programming Challenges 3 points each Create a new class P1Guess and follow Challenge Lab 5 located here. import java.util.scanner; 1) P1Guess.java import java.util.random; //needed for Random class import java.util.scanner; //needed for Scanner class public class P1Guess public static void main(string [] args) //create Scanner object and Random class object Scanner keyboard = new Scanner (System.in); Random generator = new Random(); //declare variables int userguess, secret, count=0; //get a random secret number secret = generator.nextint(100); System.out.println("I've got a number 0 to 100."); //gets the user's guess and compares to secret do System.out.println("What's your guess?"); userguess = keyboard.nextint(); if (userguess == secret) System.out.println("Got it!"); else if (userguess > secret) System.out.println("Guess lower."); else System.out.println("Guess higher."); count++; while (userguess!= secret); System.out.println("That took you " + count + " tries.");

2) Create a new class called P2Hotel and solve the following: /** * Andrew Fiola * 2012.02.27 * * Calculates a hotel's total occupied and vacant rooms after taking user input * for each floor, then calculates the hotel's occupancy rate. */ import java.util.scanner; //get keyboard scanner public class P2Hotel public static void main(string[] args) //Variable declarations int totalfloors; //total number of floors in hotel int totalrooms = 0; //total number of rooms in hotel int roomsentered; //Temporary variable to store user input until it is validated int occupiedrooms = 0; //Number of occupied rooms in hotel Scanner keyboard = new Scanner(System.in); //Keyboard scanner //Prompt user for number of floors in hotel. // Will reject input if a number less than 1 is entered. System.out.println("\fHow many floors does the hotel have?"); totalfloors = keyboard.nextint(); while(totalfloors < 1) System.out.println("The hotel can't have less than one floor! Try again."); totalfloors = keyboard.nextint(); //Input loop. For each floor, gets total number of rooms and number occupied for(int currentfloor = 1; currentfloor <= totalfloors; currentfloor++) System.out.println("How many rooms are on floor " + currentfloor + "?" + " Please enter at least 10.");

roomsentered = keyboard.nextint(); while(roomsentered < 10) System.out.println("Invalid. Please enter at least 10."); roomsentered = keyboard.nextint(); totalrooms += roomsentered; System.out.println("How many rooms on this floor are occupied?"); roomsentered = keyboard.nextint(); while(roomsentered < 0) System.out.println("You can't have a negative number of occupied rooms!" + " Try again."); roomsentered = keyboard.nextint(); occupiedrooms += roomsentered; //Output results System.out.println("Results:"); System.out.println("The hotel has " + totalrooms + " rooms."); System.out.println("Of those, " + occupiedrooms + " are occupied and " + (totalrooms - occupiedrooms) + " are vacant."); System.out.println("The hotel's occupancy rate is " + (((double)occupiedrooms / totalrooms) * 100) + "%."); 3) Create a new class called P3UppercaseFile and solve the following: P3UppercaseFile.java import java.io.*; import java.util.scanner; public class P3UppercaseFile public static void main(string [] args)throws IOException Scanner keyboard = new Scanner (System.in); System.out.print("Enter the file to be read: "); String filename1 = keyboard.nextline(); File file1 = new File(filename1); Scanner inputfile = new Scanner(file1);

System.out.print("Enter the file to write: "); String filename2 = keyboard.nextline(); PrintWriter outputfile = new PrintWriter(filename2); while (inputfile.hasnext()) String lower = inputfile.nextline(); String upper = lower.touppercase(); outputfile.println(upper); //close the input file inputfile.close(); outputfile.close();