Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using...

19
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1

Transcript of Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using...

Page 1: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

Java Programming, 3eConcepts and Techniques

Chapter 3

Section 62 –

Manipulating Data Using Methods – Day 1

Page 2: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

2Chapter 3: Manipulating Data Using Methods

Objectives

• Understand the different ways to get input in Java (console application in a command prompt window, console application with dialog boxes and applets)

• Identify, declare, and use primitive data types

• Set up a new project for calculating body mass index

Page 3: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

3Chapter 3: Manipulating Data Using Methods

Introduction

• Data are collections of raw facts or figures

• A program performs operations on input data to output information

• Input data can come from a variety of sources– The program itself– Users of the program– External files

Page 4: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

4Chapter 3: Manipulating Data Using Methods

The Body Mass Index Calculator

• An interactive program– Accepts the weight and height from the user – Calculates the BMI to gauge total body fat– Displays the result

• Three versions– Input/Output using the command prompt– Input/Output using dialog boxes– Web environments use an applet interface

Page 5: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

5Chapter 3: Manipulating Data Using Methods

(b) console application using dialog boxes

(a) console application in a command prompt window

(c) applet

Page 6: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

6Chapter 3: Manipulating Data Using Methods

Page 7: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

7Chapter 3: Manipulating Data Using Methods

Problem Analysis

• Convert user input to metric measurements • Calculate the BMI • Display the result

Page 8: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

8Chapter 3: Manipulating Data Using Methods

Design the Solution

• Design the three kinds of user interfaces with storyboards

• Design the logic of the program– Use pseudocode for sequential flow for all

programs– Use an event diagram for the applet

• Validate the design– Compare the program design with the original

requirements

Page 9: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

9Chapter 3: Manipulating Data Using Methods

Storyboards

Page 10: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

10Chapter 3: Manipulating Data Using Methods

Page 11: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

11Chapter 3: Manipulating Data Using Methods

Coding the Program

• Import the java.io package– Provides classes to support system input and

output

• Add a throws IOException clause to the method header– Warns the compiler that the possibility of input

or output errors exists– Gives the program the opportunity to handle

input or output errors during run-time without aborting

Page 12: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

12Chapter 3: Manipulating Data Using Methods

Set up a new project in NetBeans

• Open NetBeans

• Create a new project called BodyMassCalculator

• Choose Java>Java Application

• Set the Main Class to bodymasscalculator.BodyMass

Page 13: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

13Chapter 3: Manipulating Data Using Methods

Coding the Program

Add the java.io.* library

Add “throws IOException”

Page 14: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

14Chapter 3: Manipulating Data Using Methods

Storing Data

• Java is a strongly typed language– Variables must be declared with a data type– Variable locations can hold only that data type

• Java has two categories of data types– Primitive data types hold single data items

• Integers, characters, floating point, and booleans are primitive types

– Reference data types hold a value that refers to the location of the data

• All Objects and arrays are reference types

Page 15: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

15Chapter 3: Manipulating Data Using Methods

Page 16: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

16Chapter 3: Manipulating Data Using Methods

Declaring Variables

Page 17: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

17Chapter 3: Manipulating Data Using Methods

Declare the Variables You Need• Add variable declarations

Page 18: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

18Chapter 3: Manipulating Data Using Methods

Summary

• Variables are simply places to store information– Variables can only store one type of data

• There are different ways to get input from the user

Page 19: Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.

19Chapter 3: Manipulating Data Using Methods

Rest of Today

• Make sure you have created the BodyMassCalculator class and added the variables you will need.