java programming, 3e concepts and techniques chapter 3 section 62 – manipulating data using...
TRANSCRIPT
Java Programming, 3eConcepts 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
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
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
5Chapter 3: Manipulating Data Using Methods
(b) console application using dialog boxes
(a) console application in a command prompt window
(c) applet
6Chapter 3: Manipulating Data Using Methods
7Chapter 3: Manipulating Data Using Methods
Problem Analysis
• Convert user input to metric measurements • Calculate the BMI • Display the result
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
9Chapter 3: Manipulating Data Using Methods
Storyboards
10Chapter 3: Manipulating Data Using Methods
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
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
13Chapter 3: Manipulating Data Using Methods
Coding the Program
Add the java.io.* library
Add “throws IOException”
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
15Chapter 3: Manipulating Data Using Methods
16Chapter 3: Manipulating Data Using Methods
Declaring Variables
17Chapter 3: Manipulating Data Using Methods
Declare the Variables You Need• Add variable declarations
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
19Chapter 3: Manipulating Data Using Methods
Rest of Today
• Make sure you have created the BodyMassCalculator class and added the variables you will need.