Introduction to Java Unit 1. Using BlueJ to Write Programs

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Programs

Full file at

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

Programming with Java

Fundamentals of Programming Data Types & Methods

Introduction to Java Applications

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

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

CEN 414 Java Programming

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

Object Oriented Programming. Java-Lecture 1

Computational Expression

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

Chapter 2: Data and Expressions

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

4. Java Project Design, Input Methods

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

COMP 202 Java in one week

Chapter 2: Data and Expressions

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

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

Control Statements: Part 1

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

Chapter 4 Lab. Loops and Files. Objectives. Introduction

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Data Conversion & Scanner Class

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

STUDENT LESSON A7 Simple I/O

Chapter 2 ELEMENTARY PROGRAMMING

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

COMP 202. Java in one week

What did we talk about last time? Examples switch statements

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

Supplementary Test 1

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

Chapter 2. Elementary Programming

Final Exam Practice. Partial credit will be awarded.

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

Lecture Set 2: Starting Java

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

St. Edmund Preparatory High School Brooklyn, NY

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

Data Types and the while Statement

Lecture Set 2: Starting Java

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

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

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

Getting started with Java

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

First Java Program - Output to the Screen

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

Lecture 8 " INPUT " Instructor: Craig Duckett

Unit 4: Classes and Objects Notes

COMP 110/L Lecture 5. Kyle Dewey

Intro to Event-Driven Programming

DATA TYPES AND EXPRESSIONS

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

1 Short Answer (15 Points Each)

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

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.

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming

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

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.

MODULE 02: BASIC COMPUTATION IN JAVA

Midterms Save the Dates!

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

Text User Interfaces. Keyboard IO plus

Introduction to Computer Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Topic 4 Expressions and variables

Midterm Examination (MTA)

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

Peer Instruction 1. Elementary Programming

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

Chapter 5 Lab Methods

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

Computer Science is...

Lab 2.1: Fixing a C++ program

Building Java Programs

Basic Programming Elements

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Program Fundamentals

Darrell Bethea May 25, 2011

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Programming Exercise 7: Static Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

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

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

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

Chapter. Let's explore some other fundamental programming concepts

Introduction to Java & Fundamental Data Types

Chapter 2: Basic Elements of Java

Introduction to Java Applications; Input/Output and Operators

Transcription:

Introduction to Java Unit 1. Using BlueJ to Write Programs 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. In this program 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. Use an int for the number of widgets and a double for the unit price. Here s what the output should look like if the user wants to buy 7 lovely widgets at $1.50 each. Try to figure out how to print a blank line before you print the results. Eighth Program. Have the user enter three numbers and store them as doubles. Then display the sum of the three numbers and the average of the three numbers. Here is an example of how the program should run. Enter a number: User enters 5.5 Enter a second number: User enters 6.1 Enter a third number: User enters 4.3 The sum of those numbers is 15.899999999999999 The average of those numbers is 5.3 Notice that when working with doubles, small errors can appear. These errors are the unavoidable results of how decimals are stored in a computer. Ninth Program. The user enters a number and the program doubles that number three times. Use an int to store the number. The program should run like this: Enter a number User enters a 3 3 doubled in 6 6 doubled in 12 12 doubled in 24