Presentation is loading. Please wait.

Presentation is loading. Please wait.

04 WeightLoss program1May 15 04 WeightLoss program CE00858-1: Fundamental Programming Techniques.

Similar presentations

Presentation on theme: "04 WeightLoss program1May 15 04 WeightLoss program CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 04 WeightLoss program1May WeightLoss program CE : Fundamental Programming Techniques

2 04 WeightLoss program2May 15 Objectives In this session, we will: analyse a problem that uses calculations implement the solution using concepts introduced so far

3 04 WeightLoss program3May 15 WeightLoss specification calculate a user’s weight loss based on the amount of exercise they take user specifies full name and hours spent cycling, running and swimming output first name, total calories burnt and pounds lost person loses 1 pound for every 3500 calories burnt TaskCalories burnt per hour Cycling200 Running475 Swimming275

4 04 WeightLoss program4May 15 Analysis what data is used? what operations are performed? some of these operations may need further analysis

5 04 WeightLoss program5May 15 //calculate and output weight loss given time spent on activities import java.util.*; public class WeightLoss { public static void main (String args[]) { //create Scanner for input //declare constants for calories per activity and calories burnt //prompt for name and input response //prompt for hours spent exercising and input responses //determine first name //calculate calories burnt //calculate weight loss //output name, calories burnt and weight loss }

6 04 WeightLoss program6May 15 Code snippets declaring constants: final int CYCLING_CALS = 200; //constants capitalised input name as string: String fullname = myKeyboard.nextLine(); input time spent doing activities as doubles: double cycling = myKeyboard.nextDouble(); determine first name by extracting characters before first space: int spacePosition = fullname.indexOf(" "); String firstName = fullname.substring(0, spacePosition);

7 04 WeightLoss program7May 15 Testing enter 0 for amount of time spent on each activity Calories burnt: 0.0, Weight loss: 0.0 pounds enter 10 for cycling, 2 for running and 2 for swimming Calories burnt: , Weight loss: 1.0 pound

8 04 WeightLoss program8May 15 Summary In this session we have: analysing a problem to determine what is required implementing a solution that: uses constants has data of different types inputs data from the user manipulates strings performs calculations In the next session we will: look at choices in programs

Download ppt "04 WeightLoss program1May 15 04 WeightLoss program CE00858-1: Fundamental Programming Techniques."

Similar presentations

Ads by Google