AP Computer Science Unit 1. Writing Programs Using BlueJ

Similar documents
AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Programs

Fundamentals of Programming Data Types & Methods

Full file at

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

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

COMP 202 Java in one week

Programming with Java

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

Introduction to Java Applications

St. Edmund Preparatory High School Brooklyn, NY

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

Computational Expression

COMP 202. Java in one week

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

Peer Instruction 1. Elementary Programming

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

Chapter 2: Data and Expressions

4. Java Project Design, Input Methods

Chapter 2: Data and Expressions

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

Object Oriented Programming. Java-Lecture 1

Midterms Save the Dates!

Introduction to Computer Science Unit 2. Exercises

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

COMP 110/L Lecture 5. Kyle Dewey

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

Data Conversion & Scanner Class

Mr. Monroe s Guide to Mastering Java Syntax

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Supplementary Test 1

Chapter 5 Lab Methods

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

AP CS Unit 3: Control Structures Notes

CEN 414 Java Programming

Lecture 8 " INPUT " Instructor: Craig Duckett

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Introduction to Java (All the Basic Stuff)

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

Unit 4: Classes and Objects Notes

Control Statements: Part 1

Computer Science is...

Text User Interfaces. Keyboard IO plus

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

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.

Software and Programming 1

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

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

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

What did we talk about last time? Examples switch statements

Chapter. Let's explore some other fundamental programming concepts

AP Computer Science A Unit 2. Exercises

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

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

1 Short Answer (15 Points Each)

First Java Program - Output to the Screen

Intro to Event-Driven Programming

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

Chapter 5 Lab Methods

Lecture Set 2: Starting Java

Name: Checked: Access the Java API at the link above. Why is it abbreviated to Java SE (what does the SE stand for)?

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

Introduction to Computer Science Unit 2. Notes

COMP 202 Java in one week

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

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

Java Coding 3. Over & over again!

CS 211: Existing Classes in the Java Library

Lecture Set 2: Starting Java

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

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

AP Computer Science A

Software and Programming 1

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

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:

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

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming

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.

Getting started with Java

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

Chapter 2 ELEMENTARY PROGRAMMING

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

Software and Programming 1

Lab 2.1: Fixing a C++ program

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

ECE 161 WEEK 4 Introduction to Programing in Java

Using Classes and Objects

Final Exam Practice. Partial credit will be awarded.

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

Classes and Objects Part 1

Name: Checked: Access the Java API at the link above. Why is it abbreviated to Java SE (what does the SE stand for)?

Chapter 5 Lab Methods

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Practice Midterm 1 Answer Key

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 { The class name must start with a letter and have no spaces. public static void main(string[] args) { System.out.println( "Hello World" ); All of our programs will have a main method. This statements causes the phrase in quotes to be displayed.

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 rightclicking 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. import java.util.scanner; 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 base 1 and 2.9 for base 2. 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.

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. Your program should look something like the one to the right (but should obviously work for any valid integer value). 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. You will need to use the mod operator. Two examples are shown below. Eighth Program. One flower costs $7 but a dozen flowers cost $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 and the program then divides that number in half. Then divides the new number in half. And repeats this a total of four times. For example, if the user enters 20 then the program should display the following: Half of 20.0 is 10.0 Half of 10.0 is 5.0 Half of 5.0 is 2.5 Half of 2.5 is 1.25 Tenth Program. Write a program where the user enters three numbers and the program displays the average of these three numbers. Use the following code in your program. Scanner scan = new Scanner( System.in ); System.out.println( "Enter three integers" ); int n1 = scan.nextint(); int n2 = scan.nextint(); int n3 = scan.nextint();