AP Computer Science Unit 1. Programs

Similar documents
Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Computer Science Unit 2. Notes

Chapter 2: Data and Expressions

Full file at

Computational Expression

Fundamentals of Programming Data Types & Methods

COMP 202 Java in one week

Supplementary Test 1

Chapter 2: Data and Expressions

Introduction to Computer Science Unit 2. Notes

Midterm Examination (MTA)

AP CS Unit 3: Control Structures Notes

Welcome to the Using Objects lab!

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

STUDENT LESSON A7 Simple I/O

Programming with Java

1 Short Answer (15 Points Each)

Final Exam Practice. Partial credit will be awarded.

AP Computer Science A

Data Conversion & Scanner Class

COMP-202: Foundations of Programming

Introduction to Computer Science Unit 2. Exercises

What did we talk about last time? Examples switch statements

AP Computer Science A Unit 2. Exercises

Welcome to the Using Objects lab!

Chapter 2: Review Exercise Solutions R2.1

Basic Computation. Chapter 2

COMP 202 Java in one week

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

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

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

Elementary Programming

Lecture Set 2: Starting Java

Lecture 8 " INPUT " Instructor: Craig Duckett

Object Oriented Programming. Java-Lecture 1

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Set 2: Starting Java

ing execution. That way, new results can be computed each time the Class The Scanner

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

St. Edmund Preparatory High School Brooklyn, NY

1 Short Answer (5 Points Each)

Anatomy of a Java Program: Comments

Formatting Output & Enumerated Types & Wrapper Classes

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.

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CIS133J. Working with Numbers in Java

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

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

Getting started with Java

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

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Primitive Data, Variables, and Expressions; Simple Conditional Execution

COMP 202. Java in one week

Chapter 2. Elementary Programming

Practice Midterm 1. Problem Points Score TOTAL 50

Java Classes: Math, Integer A C S L E C T U R E 8

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

First Java Program - Output to the Screen

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

Welcome to the Primitives and Expressions Lab!

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

! 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

CSE Module 1. A Programming Primer 1/23/19 CSE 1321 MODULE 1 1

4. Java Project Design, Input Methods

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Unit 4: Classes and Objects Notes

1. Download the JDK 6, from

Introduction to Java Applications

Peer Instruction 1. Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

Program Fundamentals

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

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

HALF-YEARLY EXAMINATIONS FEBRUARY Subject: Computing Form: 4 Time: 1 ½ hours MARKING SCHEME

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

Darrell Bethea May 25, 2011

CEN 414 Java Programming

Java Coding 3. Over & over again!

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Control Statements: Part 1

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

CS 302: Introduction to Programming

1 Short Answer (10 Points Each)

PROGRAMMING FUNDAMENTALS

Using Java Classes Fall 2018 Margaret Reid-Miller

Conditional Execution

Computer Science is...

Midterms Save the Dates!

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

Transcription:

AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated for you. It is a little misleading because we will NOT add code where it says /* ADD YOUR CODE HERE */. We could do that but we won t. Delete everything in green. Change the code so that it matches the code shown. Save the file onto the desktop (or flash drive). Click the compile tab. Fix any errors. Recompile. Click Run and you should see Hello World displayed in the Interactions panel at the bottom.

public class First { public static void main(string[] args) { System.out.println( "Hello World" ); System.out.println is a magical statement that causes something to be displayed in the Interactions panel. "Hello World" is called a string and System.out.println displays it as written. Create a new class file (name the class Second ) and enter the following code. Compile and run the program. Notice that: The plus sign is used to join the string literal and the value of the variable. The result is what System.out.println displays. System.out.println displays the value of a variable only if the variable is not enclosed by quotes. Create another class file and name the class Third. Enter this code. Compile and run it. A discussion of the code follows on the next page.

import java.util.scanner; public class Third { public static void main(string[] args) { Scanner kb = new Scanner( System.in ); A System.out.println( "Enter an integer:" ); int x = kb.nextint(); B System.out.println( "You entered a " + x ); A) This statement declares a variable, kb, which is of type Scanner. The expression on the right side of the assignment operator creates a Scanner object. The name of the variable can change but keep everything else the same. B) The expression to the right of the assignment operator calls the nextint method which retrieves the first integer entered by the user. This value is then assigned to the variable. Create another file and enter this code. Compile and run the program. Enter 7.8 for the decimal and 6 for the integer. The following message should be displayed: num1 is 7.8, num2 is 0 Please ask if you do not understand why num2 is equal to zero. Notice: We use the nextdouble method if we expect the user to enter a decimal. Since I changed the name of the Scanner variable to x, then I must use x when calling the nextdouble and nextint methods.

1. Create a new file and name it "Problem." Copy the code below and run it. public class Problem { public static void main( String [] args ) { double x = 7.5; double y = 5.4; Correct The program s answer is answer is... System.out.println( x ); System.out.println( y ); double z = x + y; System.out.println( z ); z = x - y; System.out.println( z ); Notice that sometimes the results contain small errors. Here s why: all data are represented as a series of ones and zeros (binary form). Certain decimal values cannot be represented exactly in binary form and so small floating point errors can appear. We will not concern ourselves with these errors but you should be aware that they can occur. 2. This program will implement the formula for calculating the area of a trapezoid: ( base ) * 1 base area height 2 2 Create a new file. In the file, use the Scanner class to get the height and base measurements. Then calculate and display the correct area. The variables representing the height and bases should be ints (this is an arbitrary decision by me) but the area should be a double. For example, if the height is 5 units long and the bases are 3 and 2, then your program should display the correct area: 12.5 square units. 3. This program in which the user enters the number of widgets ordered and the unit price. The program calculates and displays: the cost of the widgets, the 6% tax on the sale, and the total cost. Don't worry about rounding off the results to two places. Yes, you should create a new file. You should use an int for the number of widgets because that is the logical data type.

4. This program should convert degrees Fahrenheit to degrees Celsius. The formula is: 5 C F 32 9 Yes, you create a new file. Fahrenheit should be an integer value but the result is a decimal. 5. Write a program that runs like this - the user enters a number, let s say 10, and the program displays: Half of 10 is 5 Half of 5 is 2.5 Half of 2.5 is 1.25 Half of 1.25 is 0.625 so on. If the user enters a different number then it display half of that, then half of the half, and 6. Write a program where the user enters a positive integer that represents a certain number of hours. The program then displays a message that displays the number in terms of days and hours. For example: if the user enters 51, then the program displays: 51 hours = 2 days and 3 hours If the user enters 18, the program displays: 18 hours = 0 days and 18 hours. You will need to use the mod operator. 7. One flower costs $7 but a dozen flowers cost only $70. Write a program where the user enters the number of flowers and the program displays the cost. For example, if the user enters 27 then the program displays: 27 flowers will cost $161 If the user enters 11 then the program displays: 11 flowers will cost $77