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

Similar documents
CSIS 10A Assignment 4 SOLUTIONS

Loops. CSE 114, Computer Science 1 Stony Brook University

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

Object Oriented Programming. Java-Lecture 1

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

Chapter 4: Conditionals and Recursion

1. An operation in which an overall value is computed incrementally, often using a loop.

AP CS Unit 3: Control Structures Notes

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

Introduction to Computer Science Unit 2. Notes

! 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

2.2 - Making Decisions

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

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Assignment 2.4: Loops

CONDITIONAL EXECUTION

Example Program. public class ComputeArea {

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

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

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Introduction to Computer Science Unit 2. Exercises

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

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

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Chapter 4: Control Structures I

Introduction to Computer Science Unit 2. Notes

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Building Java Programs

CS 101 Spring 2007 Midterm 2 Name: ID:

CS1150 Principles of Computer Science Loops (Part II)

Combined Assignment Operators. Flow of Control. Increment Decrement Operators. Operators Precedence (Highest to Lowest) Slide Set 05: Java Loops

Introduction to Computer Programming

Java Coding 3. Over & over again!

Basic Control Structures

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Building Java Programs

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

Repetition, Looping. While Loop

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

Controls Structure for Repetition

CSIS 10A Assignment 4 SOLUTIONS

Building Java Programs

Midterm Examination (MTA)

Question: Total Points: Score:

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

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

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

Building Java Programs

Write a program which converts all lowercase letter in a sentence to uppercase.

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

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

The action of the program depends on the input We can create this program using an if statement

Important Java terminology

4CCS1PRP, Programming Practice 2012 Lecture 6: Arrays - Part 1

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

Over and Over Again GEEN163

Lab 9: Creating a Reusable Class

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

AP Computer Science Unit 1. Programs

Building Java Programs

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

Topic 11 Scanner object, conditional execution

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

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

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

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Java I/O and Control Structures

Building Java Programs

Practice Midterm 1. Problem Points Score TOTAL 50

Chapter 3. Selections

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

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

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

University of Palestine. Mid Exam Total Grade: 100

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

JAVA OPERATORS GENERAL

Lecture 14. 'for' loops and Arrays

Loops. GEEN163 Introduction to Computer Programming

Arrays. Eng. Mohammed Abdualal

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

Conditional Execution

1 Definitions & Short Answer (4 Points Each)

Oct Decision Structures cont d

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

In this chapter, you will:

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Some Sample AP Computer Science A Questions - Solutions

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

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Java I/O and Control Structures Algorithms in everyday life

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS171:Introduction to Computer Science II

Building Java Programs

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

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

CS 101 Fall 2005 Midterm 2 Name: ID:

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

Transcription:

Lab1 Solution MathTrick.java /** * MathTrick Lab 1 * * @version 8/25/11 * Completion time: 10-15 minutes public class MathTrick public static void main(string [] args) int num = 34; //Get a positive integer int num2 = num * num; //Square it num2 = num + num2; //Add the original number num2 = num2 / num; //Divide by the original number num2 = num2 + 17; //Add 17 num2 = num2 - num; //Subtract the original number num2 = num2 / 6; //Divide by 6 System.out.println("Here is the final number, which will be three, unless you broke the universe:"); //Print the result /* Post Lab Assessment int a=4; int b=5; int c=6; a = a * 2 ; b = b + a ; c = b - a ; a = c + b ; // a 8 // b 13 // c 5 // a 18 Lab2 Solution CoinFlip.java //Date : 2/8/2011 //References: None public class CoinFlip public static void main(string [] args) //Flush the buffer System.out.println("\f"); //Get a random number double my1stnumber; my1stnumber = Math.random();

//Decide the outcome of a coin flip int score1; if (my1stnumber <.45) System.out.println("The first flip was heads"); score1 = 10; else if (my1stnumber <.90) System.out.println("The first flip was tails"); score1 = 5; else if (my1stnumber <.95) System.out.println("The first flip landed on the edge!"); System.out.println("You are one lucky dude."); score1 = 20; else System.out.println("The first flip fell off the table"); score1 = 1; System.out.println("Score for the first flip was: " + score1); //Get a second random number double my2ndnumber; my2ndnumber = Math.random(); //Decide the outcome of a second coin flip int score2; if (my2ndnumber <.45) System.out.println("The second flip was heads."); score2 = 10; else if (my2ndnumber <.90) System.out.println("The second flip was tails."); score2 = 5; else if (my2ndnumber <.95) System.out.println("The second flip landed on the edge!"); System.out.println("You are one lucky dude."); score2 = 20; else System.out.println("The second flip fell off the table."); score2 = 1; System.out.println("Score for the second flip was: " + score2); //Print out which flip had a higher score if (score1 > score2) System.out.println("The first flip had the higher score."); else if (score2 > score1) System.out.println("The second flip had the higher score."); else System.out.println("Both flips had the same score.");

int maxscore = Math.max(score1, score2); //Print out the maximum of the two flips if (score1!= score2) System.out.println("The higher score was " + maxscore); else System.out.println("And they are " + score1 + " each."); POSTLAB /* Post Lab Assessment. Type in your answers for these problems relating to the flowchart (shown below) 1) Follow the flowchart to determine the charge if the variable Age contains the value: Age: a) 5 b) 24 c) 53 d) 90 Charge: a) 7 b) 10 c) 10 d) 5 2) The following Java code attempts to reproduce the flow chart, but it contains some errors. (assume all variables are already declared and initialized). What will Charge now be for Age values: Age: a) 5 b) 24 c) 53 d) 90 Charge: a) 10 b) 10 c) 10 d) 5 if (Age < 65) Charge = 10; else if (Age < 16) Charge = 7; else Charge = 5; 3) Copy the Java statements above and paste below this line. Then correct the code so it matches the flowchart below. if (Age < 16) Charge = 7; else if (Age < 65) Charge = 10; else Charge = 5;

Lab3 Solution Calculator.java import java.util.*; public class Calculator public static void main (String [] args) //Flush the buffer System.out.println("\f"); //Create a Scanner object so we can get user input Scanner input = new Scanner(System.in); //Welcome the user System.out.println("Welcome to CSIS10A's calculator."); System.out.println("Please enter your first number."); //Get a double value from the user String value1 = input.nextline(); double number1 = Double.parseDouble(value1); //Get another double value from the user System.out.println("Please enter your second number."); String value2 = input.nextline(); double number2 = Double.parseDouble(value2); //Ask the user what arithmetic operation we are doing (store it as an int) System.out.println("What do you want to do with these numbers?"); System.out.println(" 1)Add"); //add the numbers you entered together System.out.println(" 2)Subtract"); //subtract them System.out.println(" 3)Multiply"); //multiply them System.out.println(" 4)Divide"); //divide them System.out.println("Please enter a number between 1,2,3 and 4 for the corresponding action"); String value3 = input.nextline(); int userchoice = Integer.parseInt(value3); double result; //Calculate and print out the answer if (userchoice == 1) result = number1 + number2; else if (userchoice == 2) result = number1 - number2; else if (userchoice == 3) result = number1 * number2; else if (userchoice == 4) result = number1 / number2; else // What if the users don't enter a number between 1,2,3 and 4 System.out.println("I am really sorry.");

System.out.println("The number you entered was not among all the choices."); System.out.println("Only 1,2,3 or 4 is accepted"); System.out.println("Please exit the program and run it again."); System.out.println("Thank you for using the calculator."); //End the program. POSTLAB /** MARK THE ERRORS! Find all 8 (or more?), if you dare! * * A program that reads an age that is typed in by the * user and insults them import java.util.*; public class AgeInsult public static void main(string args) XXXX need [] after String Scanner in = new Scanner(System.in); System.out.println("How old are you?"); int age = in.nextdouble(); XXXXX type mismatch, use in.nextint() //Exactly one of these 4 main blocks will be executed. if (age <= 3) System.out.println("Too young to know any better.") XXXXXXX need close curly else if (age < 10) Sytem.out.println(Not ready for prime time.); XXXX need 's' after 'y' in Sytem, and " " around text else if (age < 18) System.out.println("Grow up."); else (age >=18) XXXXXXX never put a condition after else. else means all the above conditions are false System.out.println("Get a job!"); //if statement without "else if" or even an "else". if (age > 120) System.out.println("Way too old! You are probably lying."); XXXXXX need a close curly for class AgeInsult