Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard



Similar documents
1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

AP Computer Science Static Methods, Strings, User Input

Chapter 2: Elements of Java

Chapter 2 Elementary Programming

Introduction to Java

Introduction to Java. CS 3: Computer Programming in Java

Chapter 3. Input and output. 3.1 The System class

MAT 2170: Laboratory 3

Sample CSE8A midterm Multiple Choice (circle one)

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Part I. Multiple Choice Questions (2 points each):

JAVA ARRAY EXAMPLE PDF

Some Scanner Class Methods

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Using Files as Input/Output in Java 5.0 Applications

Object-Oriented Programming in Java

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

Programming Fundamentals I CS 110, Central Washington University. November 2015

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CS 106 Introduction to Computer Science I

Install Java Development Kit (JDK) 1.8

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

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Basics of Java Programming Input and the Scanner class

The following program is aiming to extract from a simple text file an analysis of the content such as:

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

AP Computer Science Java Mr. Clausen Program 9A, 9B

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Lab 5: Bank Account. Defining objects & classes

I = V/r P = VI. I = P/V = 100 W / 6 V = amps. What would happen if you use a 12-volt battery and a 12-volt light bulb to get 100 watts of power?

Introduction to Programming

Chapter 2 Introduction to Java programming

Building Java Programs

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Topic 11 Scanner object, conditional execution

6.1. Example: A Tip Calculator 6-1

Comp 248 Introduction to Programming

Building Java Programs

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

(Eng. Hayam Reda Seireg) Sheet Java

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

1st Grade Math Standard I Rubric. Number Sense. Score 4 Students show proficiency with numbers beyond 100.

More on Objects and Classes

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Programmierpraktikum

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Building Java Programs

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Visit us at

6. the result you get when you divide fifteen by four times a number. Hk 2, 105 Written problems

Chapter One Introduction to Programming

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Java Basics: Data Types, Variables, and Loops

Arrays in Java. Working with Arrays

BANK B-I-N-G-O. service charge. credit card. pen. line nickel interest cash bank. ATM dollar check signature. debit card.

Week 1: Review of Java Programming Basics

Java CPD (I) Frans Coenen Department of Computer Science

J a v a Quiz (Unit 3, Test 0 Practice)

LOOPS CHAPTER CHAPTER GOALS

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

College of Charleston Math Meet 2008 Written Test Level 1

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Advanced GMAT Math Questions

Introduction to Visual C++.NET Programming. Using.NET Environment

Contents. 9-1 Copyright (c) N. Afshartous

1) (-3) + (-6) = 2) (2) + (-5) = 3) (-7) + (-1) = 4) (-3) - (-6) = 5) (+2) - (+5) = 6) (-7) - (-4) = 7) (5)(-4) = 8) (-3)(-6) = 9) (-1)(2) =

CS 121 Intro to Programming:Java - Lecture 11 Announcements

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Programming and Data Structures with Java and JUnit. Rick Mercer

Example of a Java program

Grade 2 Level. Math Common Core Sampler Test

Using Two-Dimensional Arrays

Java Crash Course Part I

CSE 8B Midterm Fall 2015

Lecture 5: Java Fundamentals III

Course Intro Instructor Intro Java Intro, Continued

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Judi Kinney, Author Jo Reynolds, Illustrations and Graphic Design

Verbal Phrases to Algebraic Expressions

13 File Output and Input

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Math BINGO MOST POPULAR. Do you have the lucky card? B I N G O

Programming Languages CIS 443

Math Questions & Answers

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

The Properties of Signed Numbers Section 1.2 The Commutative Properties If a and b are any numbers,

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Interactive Applications (CLI) and Math

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Mathematics Florida Standards (MAFS) Grade 2

Math 55: Discrete Mathematics

Electrical Design TABLE OF CONTENTS FOREWORD BASIC ELEMENTS OF ELECTRICITY... 2 Electricity works much like water Ohm s Law

Transcription:

INPUT & OUTPUT I/O Example Using keyboard input for characters import java.util.scanner; class Echo{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard String indata; // String variable } } System.out.println("Enter the data:"); indata = sc.nextline(); // read a line from scanner System.out.println("You entered: " + indata ); I/O Example Using keyboard input for Integers import java.util.scanner; class EchoSquare{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard int num, square; // declare two int variables System.out.println("Enter an integer:"); num = sc.nextint(); // read an int from scanner square = num * num ; // compute the square } } System.out.println("The square of " + num + " is " + square);

Exercise 11 Area of a Circle Write a program that calculates the area of a circle from its radius. The radius will be an integer read in from the keyboard. The user dialog will look like this: D:\users\default>java CircleArea Input the radius: 3 The radius is: 3 The area is: 28.274333882308138 You will need to use the constant PI which you get by using Math.PI

Exercise 12 Cents to Dollars Write a program that reads in a number of cents. The program will write out the number of dollars and cents, like this: D:\users\default>java Dollars Input the cents: 324 That is 3 dollars and 24 cents. (For this program you will use integer arithmetic and will need to avoid floating point arithmetic. Review the integer remainder operator % if you are unsure how to proceed.)

Exercise 13 Correct Change When cashiers in a store give you change they try first to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents: One dollar fits into 163, leaving 63 cents. Two quarters fit into 63 cents, leaving 13 cents. One dime fits into 13 cents, leaving 3 cents. No nickels are needed. Three cents are left. Your change is : 1 dollar, two quarters, one dime, and three cents. Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. All variables and all math in this program will be integers. If you are stuck, it will help to do an example problem with paper and pencil.

Exercise 14 Ohm's Law Ohm's law relates the resistance of a electrical device (like a heater) to the electric current flowing through the device and the voltage applied to it. The law is: I = V/R Here, V is the voltage (measured in volts), I is the current (measured in amps), and R is the resistance (measured in ohms.) Write a program that asks the user for the voltage and the resistance of a device. The program will then write out the current flowing through it. Use floating point math. Since V and R are integers (converted from user input) you must use a trick to do floating point division. Change the equation to this: I = (V + 0.0)/R The math inside parentheses is done first. So V + 0.0 is done first, and since 0.0 is floating point, so will be the result.

FLOATING POINT INPUT Exercise 15 Power Costing Write a program that calculates the annual cost of running an appliance. The program will ask the user for the cost per kilowatt-hour and the number of kilowatt-hours the appliance uses in a year: Enter cost per kilowatt-hour in cents 8.42 Enter kilowatt-hours used per year 653 Annual cost: 54.9826

Exercise 16 Another Brick in the Wall When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The speed v is given by v = (1/2) g t 2 Here v is the speed in feet per second, t is the time in seconds, and g is 32.174. Write a program that asks the user for the number of seconds and then prints out the speed. Enter the number of seconds 5.4 Speed of the brick: 469.092 feet per second One hundred miles per hour is 146.67 feet per second. Use your program to determine approximately how long it takes the brick to reach that speed.

Exercise 17 Logarithms The base 2 logarithm of a number is defined by: log 2 X = n if 2 n = X For example log 2 32 = 5, because 2 5 = 32 log 2 1024 = 10, because 2 10 = 1024 Write a program that inputs a number and outputs its base 2 logarithm. Use floating point input. This problem would be easy, but the Math package does not have a base 2 logarithm method. Instead you have to do this: log 2 X = (log e X) / (log e 2) Here, log e X is the natural logarithm of X. Use this function in the Java Math package: Math.log( X ) When you use this, X must be a double. Write the program so that the user can enter floating point numbers. Enter a double: 998.65 Base 2 log of 998.65 is 9.963835330516641

Exercise 18 Harmonic Mean The harmonic mean of two numbers is given by: H = 2 / ( 1/X + 1/Y ) This is sometimes more useful than the more usual average of two numbers. Write a program that inputs two numbers (as floating point) and writes out both the usual average (the arithmetic mean) and the harmonic mean. Enter X: 12 Enter Y: 16 Arithmetic mean: 14.0 Harmonic mean: 13.714285714285715