AP Computer Science Unit 1. Writing Programs Using BlueJ

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. Programs

Full file at

Fundamentals of Programming Data Types & Methods

Programming with Java

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

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

Computational Expression

Introduction to Java Applications

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

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

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

Object Oriented Programming. Java-Lecture 1

COMP 202 Java in one week

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

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

Lesson 10: Quiz #1 and Getting User Input (W03D2)

COMP 202 Java in one week

CEN 414 Java Programming

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions

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

Supplementary Test 1

Lesson 3: Accepting User Input and Using Different Methods for Output

4. Java Project Design, Input Methods

St. Edmund Preparatory High School Brooklyn, NY

Lecture Set 2: Starting Java

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

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

Lecture Set 2: Starting Java

Getting started with Java

First Java Program - Output to the Screen

Unit 4: Classes and Objects Notes

COMP 110/L Lecture 5. Kyle Dewey

What did we talk about last time? Examples switch statements

COMP 202. Java in one week

Control Statements: Part 1

Data Conversion & Scanner Class

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Connecting with Computer Science, 2e. Chapter 15 Programming II

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.

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Midterms Save the Dates!

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

Text User Interfaces. Keyboard IO plus

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

AP CS Unit 3: Control Structures Notes

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

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

Lecture 8 " INPUT " Instructor: Craig Duckett

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Lab 2.1: Fixing a C++ program

Chapter 5 Lab Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

STUDENT LESSON A7 Simple I/O

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

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

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

Chapter 2 ELEMENTARY PROGRAMMING

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.

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

Chapter 2: Basic Elements of Java

AP Computer Science A Unit 2. Exercises

1 Short Answer (15 Points Each)

Final Exam Practice. Partial credit will be awarded.

Welcome to the Primitives and Expressions Lab!

Chapter 2. Elementary Programming

CS 211: Existing Classes in the Java Library

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

1. Look carefully at the program shown below and answer the questions that follow.

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

Introduction to Computer Science Unit 2. Notes

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

AP Computer Science A

Programming with Java

We now start exploring some key elements of the Java programming language and ways of performing I/O

Chapter 5 Lab Methods

Basic Computation. Chapter 2

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Program Fundamentals

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

Introduction to Java Applications; Input/Output and Operators

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter. Let's explore some other fundamental programming concepts

Computer Science is...

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

2.8. Decision Making: Equality and Relational Operators

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Software and Programming 1

Peer Instruction 1. Elementary Programming

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

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

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Welcome to the Using Objects lab!

Transcription:

AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save your work. (In this example I saved to the desktop don t you do that.) Enter Unit 1 where it says Folder name and then click the Create button. 2. After you ve clicked Create button, the screen to the right should appear. If you double-click on the file icon you ll see that it s a Read- Me file. This can be a useful way to document each project. Use it if you want but it is not a requirement. 3. Click on the New Class button and window appears. Enter First and click OK.

4. Your screen should look like this: Double-click on the First icon. 5. Delete everything in the First file. Then type in the following code so that it looks like the figure to the right. Java is case-sensitive so be careful when typing. 6. Click the Compile button. If the message Class compiled no syntax errors appears (as shown above, then Great, you got it right. All java programs must successfully compile before they can be run. If there were errors, fix them.

7. Now go back to the earlier window and right-click on the First icon and select void main(string[] args). The window below will appear. Do not enter anything into the textbox. Click the OK button. The window below should appear. public class First { public static void main(string[] args) { System.out.println( "Hello World" );

Second Program. Click on the New Class button. Name the class Second and click OK. Double-click on the icon labelled Second. Delete all the existing code and replace it with the following. Click Compile. If there are errors, fix them and click Compile again. Notice that + has two different meanings. In the second line of the main method it concatenates the phrase in quotes with the value of the variable. In the third line + means addition. Go to the main window. Right-click on the icon labelled Second and select the main method. Click OK and you should see this Terminal window. You may also see Hello World if you ran the two programs back to back without clearing the Terminal window first.

Third Program. Create a class, named Third, and enter this code. Compile it and run the program (by right-clicking the icon and calling the main method). If you enter 4 for the integer and 8.4 for the decimal, your Terminal window should look like this. This program shows us how to create a Scanner object. A Scanner object has methods that allow us to read information into our program. We will cover objects and methods in great detail starting in Unit 4. Here s what you need to know for now. 1) If you want the user to enter information into your program, the first line of your program will be: import java.util.scanner; This tells the compiler that the program needs to know about the Scanner class and that information can be found in a package named java.util. 2) To create a Scanner object you will use a statement like this: Scanner variable_name = new Scanner( System.in ); The variable name must follow the same rules as we discussed on the first page of the notes (i.e. start with a letter, etc.) 3) If you want the user to enter an integer, you must call the nextint method. If you want the user to enter a decimal, you must call the nextdouble method. 4) When you call nextint or nextdouble, you must use the variable name of the Scanner object and the method will be on the right side of an assignment statement. And don t forget the parentheses that s a signal to the compiler that nextint and nextdouble are methods.

Fourth Program. In this program the user will enter the length and width of a rectangle and the program will calculate and display the area and perimeter of the rectangle. Copy the outline below, fill in the blanks, and get the program to run. public class Rectangle{ public static void main( String [] args ){ Scanner sam = new Scanner( System.in ); System.out.println( "Enter the length " ); int length = sam. System.out.println( "Enter the width " ); int width =.nextint(); int area = int perimeter = System.out.println( "The area is " + area ); System.out.println( "The perimeter is " + perimeter ); Here s what your program should display if the user enters 5 and 11. Fifth Program. This program will implement the formula for calculating the area of a trapezoid: ( base ) * 1 base area height 2 2 Write a program where the user enters the height and two bases as decimals and the program calculates and displays the area. Here s what your program should display if the user enters 1.6 for the height, 2.1 for base1 and 2.9 for base2.

When should we declare a variable as an int and when should we declare it as a double? Programs run faster and more accurately with ints. So wherever possible, it is better to use an int. However, there are circumstances where you must use a double. In this class I will usually tell you which data type to use. If I don t, then use common sense. If the variable represents the number of people in a room use an int. If the variable represents an interest rate use a double. If it is ambiguous then use your best judgment. In the above paragraph I said that programs run more accurately with ints. 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. These types of errors cannot appear when working with ints. We will not concern ourselves with these errors but you should be aware that they can occur. Here s an example. The output is to the right. The third line, 12.9 is correct but the 7.5 2.1 equals 2.1 so the last line is off by 0.0000000000000004. This is obviously a very tiny error but you should be aware that they can occur. public class Floating_Point_Error{ public static void main( String [] args ){ double x = 7.5; double y = 5.4; System.out.println( x ); System.out.println( y ); double z = x + y; System.out.println( z ); z = x - y; System.out.println( z ); Sixth Program. This program should convert degrees Fahrenheit to degrees Celsius. The formula is: 5 C F 32 9 Fahrenheit should be an integer value but the result is a decimal. And just in case you need it, here are some test data: 0 Fahrenheit = -17.7777777778 Celsius 32 Fahrenheit = 0 Celsius 212 Fahrenheit = 100 Celsius

Seventh Program. 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 mode operator. Eighth Program. 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 Ninth Program. 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