CSIS 10A Assignment 4 SOLUTIONS

Similar documents
Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

AP COMPUTER SCIENCE A

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

Conditional Programming

JAVA OPERATORS GENERAL

Object Oriented Programming. Java-Lecture 1

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

Lesson 7 Part 2 Flags

Chapter 3. Selections

Anatomy of a Java Program: Comments

Over and Over Again GEEN163

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Program Control Flow

Program Control Flow

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Midterm Examination (MTA)

5) (4 points) What is the value of the boolean variable equals after the following statement?

Chapter 4: Control Structures I

1 Short Answer (10 Points Each)

Programming with Java

CS141 Programming Assignment #6

Full file at

! 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

Loops. GEEN163 Introduction to Computer Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Warmup : Name that tune!

Selenium Class 9 - Java Operators

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

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

Chapter 1 Lab Algorithms, Errors, and Testing

CS 201, Fall 2016 Sep 28th Exam 1

CSIS 10A Assignment 4 SOLUTIONS

Place your name tag here

ITERATION WEEK 4: EXMAPLES IN CLASS

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Chapter 4: Conditionals and Recursion

Loops. CSE 114, Computer Science 1 Stony Brook University

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Chapter 3 Lab Decision Structures

CS 152 Computer Programming Fundamentals The if-else Statement

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

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

Software Practice 1 Basic Grammar

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CSC 1051 Data Structures and Algorithms I

Ch. 6. User-Defined Methods

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

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

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

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Oct Decision Structures cont d

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

H212 Introduction to Software Systems Honors

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

A+ Computer Science -

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

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

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

coe318 Lab 2 ComplexNumber objects

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

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

Introduction to Computer Science Unit 2. Exercises

FOR INTERNAL SCRUTINY (date of this version: 17/2/2016) UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS

Repetition, Looping. While Loop

CS 101 Spring 2007 Midterm 2 Name: ID:

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

Data Structure and Programming Languages

IEEE Floating-Point Representation 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

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

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

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

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

download instant at

Java Programming Language. 0 A history

Full file at

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSIS 10A Practice Final Exam Solutions

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Chapter 5 Lab Methods

CONDITIONAL EXECUTION

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Decisions (If Statements) And Boolean Expressions

CS111: PROGRAMMING LANGUAGE II

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Building Java Programs

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

Algorithms and Conditionals

JAVA PROGRAMMING (340)

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Introduction to Computer Science Unit 2. Notes

Transcription:

CSIS 10A Assignment 4 SOLUTIONS Calculator.java Name: Naomi Lyles Approximate completion time: 20 minutes References: None Write a description of class Calculator here. @author (your name) @version (a version number or a date) import java.util.; public class Calculator public static void main(string []args) Create a Scanner object so we can get user input. Scanner keyboard; keyboard = new Scanner(System.in); Welcome the user System.out.println("Welcome to the calculator!"); System.out.println("What is your first number?"); Get a double value from the user double number1; number1 = keyboard.nextdouble(); Get another double value from the user System.out.println("What is your second number?"); double number2; number2 = keyboard.nextdouble(); Ask the user what arithmetic operation we are doing (store it as an int) System.out.println("What are we doing with these numbers?"); System.out.println("1) add"); System.out.println("2) subtract"); System.out.println("3) multiply"); System.out.println("4) divide"); int operation; operation = keyboard.nextint(); Calculate the answer if (operation == 1) double answer = number1 + number2; if (operation == 2) double answer = number1 - number2; if (operation == 3) double answer = number1 number2; double answer = number1 / number2; Print out the answer

Drills.java import java.util.scanner; Write a description of class Drills here. @author (your name) @version (a version number or a date) public class Drills public static void main (String [] args) System.out.println("\f"); flush the buffer (erase the screen) Scanner keyboard = new Scanner(System.in); int score; System.out.print("Enter a test score "); score = keyboard.nextint(); Ex A write a simple if statement that says "pass" if score is >= 65, otherwise it says nothing System.out.println("A) Conditional Execution"); if (score >= 65) System.out.println("pass"); / End Ex A Ex B Use an if- statement to choose one of the following: Show "pass" if score is>=65, otherwise show "fail" (use if-) Test your solution by entering different scores System.out.println("B) Alternative Execution"); if (score >= 65) System.out.println("pass"); System.out.println("fail"); / End Ex B Ex C Study the Lecture 4 slides and then write a nested if- statement that prints "A" if the variable score is >= 90, "B" if the variable score is >= 80, "C" if the variable score is >= 65, "D" if the variable score is >= 50, or "F" if none of the above are true System.out.println("C) Chained Conditionals"); if (score >= 90) System.out.println("A"); if (score >= 80) System.out.println("B"); if (score >= 65) System.out.println("C"); if (score >= 50) System.out.println("D");

System.out.println("F"); / End Ex C Ex D Write a mini program in which the user inputs an age and the output states whether or not the person is a teenager. Use nested conditionals -- first check if age is greater than 12 -- inside the code block for true, -- check if age is less than 20 Print "Hooray! A Teen!" or "sorry :(" depending on age Use the keyboard scanner we've already created to read age System.out.println("D) Nested Conditionals"); System.out.print(" What is your age?"); declare int age and read age from keyboard Scanner int age = keyboard.nextint(); write your nested if block here if (age > 12) if (age < 20) System.out.println("Hooray! A Teen!"); System.out.println("sorry :("); System.out.println("sorry :("); / End Ex D / Ex E write a void method, paycheck, below main that calculates and prints a worker's paycheck, given the number of hours, and pay rate similar to last weeks drill, but THIS TIME use an if statement to award overtime properly. paycheck( 40, 7.75); answer: $310 paycheck( 20, 9.25); answer: $185 paycheck( 50, 12.50); answer: $687.50 (the first 40 hours are paid at $12.50 the next 10 hours are at $12.501.5) paycheck( 45, 15.00); answer: $712.50 / End Ex E write your pay method here public static void paycheck(int hours, double rate) System.out.print("Hours: " + hours + " Rate: " + rate + " Paycheck: $");

double pay; if (hours <= 40) pay = hours rate; int overtimehours = hours - 40; pay = 40 rate + (overtimehours rate 1.5; System.out.println("$" + pay); System.out.format("%.02f%n", pay); Song.java Name: Naomi Lyles Approximate completion time: 15 minutes References: Lecture 4 Write a description of class Song here. @author (your name) @version (a version number or a date) public class Song public static void main(string [] args) System.out.print("\f"); beercount (99); public static void beercount(int beers) if (beers > 0) beerphrase(beers); System.out.print(" on the wall, "); beerphrase(beers); System.out.print(", ya' take one down, ya' pass it around, "); beerphrase(beers - 1); System.out.println(" on the wall."); beercount(beers - 1); System.out.println("No bottles of beer on the wall, no bottles of beer, ya can't take one down, ya' can't pass it around, 'cause there are no more bottles of beer on the wall!"); public static void beerphrase(int beers) System.out.print(beers + " bottles of beer"); BottlesOfBeer.java

Write a description of class BottlesOfBeer here. public class BottlesOfBeer public static String bottleorbottles(int n) return n == 1? "bottle" : "bottles"; public static void printbottles(int n) if (n > 0) System.out.printf("%d %s of beer on the wall, %d %s of beer, ya take one down, ya pass it around, %n",n, bottleorbottles(n), n, bottleorbottles(n)); System.out.printf("%d %s of beer on the wall.%n", n - 1, bottleorbottles(n - 1)); System.out.println("No bottles of beer on the wall, no bottles of beer, ya can t take one down, ya can t pass it"); System.out.println("around, cause there are no more bottles of beer on the wall!"); public static void beers(int n) if (n >= 0) printbottles(n); beers(n - 1); public static void main(string[] args) beers(99);

Challenge.java Name: Emma Stavropoulos Approximate completion time: 10 mins References: The online book Assignment 4 Challenge public class Challenge public static void main (String [] args) System.out.println("\f"); flush the buffer (erase the screen) verse(99); public static void bottles(int Nbottles) System.out.println(Nbottles + " bottles of beer on the wall,"); System.out.println(Nbottles + " bottles of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println((Nbottles - 1) + " bottles of beer on the wall."); System.out.println("") public static void twobottles(int Nbottles) System.out.println(Nbottles + " bottles of beer on the wall,"); System.out.println(Nbottles + " bottles of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println((Nbottles - 1) + " bottle of beer on the wall."); System.out.println(""); verse(nbottles-1); public static void onebottle(int Nbottles) System.out.println(Nbottles + " bottle of beer on the wall,"); System.out.println(Nbottles + " bottle of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println("No bottles of beer on the wall."); System.out.println(""); verse(nbottles-1); public static void nobottles() System.out.println("No bottles of beer on the wall,"); System.out.println("no bottles of beer, ya' can't take one down,"); System.out.println("ya' can't pass it around, 'cause there are no more"); System.out.println("bottles of beer on the wall!"); public static void verse(int Nmain) if (Nmain == 0) nobottles(); if (Nmain == 2) twobottles(nmain); if (Nmain == 1) onebottle(nmain); bottles(nmain); verse(nmain - 1);

Song.java Write a description of class Song here. Rachel Weber September 13, 2014 public class Song public static void main(string [] args) Displays the lyrics for the song "99 Bottle of Beer on the Wall". bottlecountdown (99); public static void bottlecountdown (int bottles) if (bottles >= 1) Prints the first three parts of each of the song's lines. System.out.print (bottles + " bottles of beer on the wall, "); System.out.print (bottles + " bottles of beer,"); System.out.print ("ya take one down, ya pass it around,"); Calculates how many bottles are left. bottles = bottles - 1; Prints the last part stating how many bottles remain. System.out.println (bottles + "bottles of beer on the wall."); Restarts the cycle with one less bottle. bottlecountdown (bottles); Prints what is sung when no bottles of beer are left. System.out.print ("No bottles of beer on the wall,"); System.out.print ("no bottles of beer, "); System.out.print ("ya can t take one down, ya can t pass it around, "); System.out.println (" cause there are no more bottles of beer on the wall!");