L07 - CS101 Lecture 7 1 Computer Science 1 1 SS C Lecture 7...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS101 Lecture 7 1 Computer Science 1 1 SS C Lecture 7 Contents Defning methods: parameters, return values, return statement Coursebook: 9 2 1 2 3 4 5 6 7 8 9 10 Number of 10 minute blocks? Look at the following program which, given the number of hours and minutes, evaluates the number of complete ten minute blocks. public class TenMinuteBlocks { public void start() { } } 13 blocks totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; int hours = 2; int minutes = 14; int totalMins, tenMinBlocks; System.out.println(tenMinBlocks + " blocks"); 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The problem Doing same calculation twice ... public class TenMinuteBlocks { public void start() { } } Repetition! 13 blocks 66 blocks totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; hours = 11; minutes = 6; int hours = 2; int minutes = 14; int totalMins, tenMinBlocks; System.out.println(tenMinBlocks + " blocks"); System.out.println(tenMinBlocks + " blocks"); 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 The problem Do same calculation three times .... public class TenMinuteBlocks { public void start() { } Repetition! 13 blocks 66 blocks 13 blocks } totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; hours = 11; minutes = 6; int hours = 2; int minutes = 14; int totalMins, tenMinBlocks; System.out.println(tenMinBlocks + " blocks"); System.out.println(tenMinBlocks + " blocks"); totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; hours = 2; minutes = 16; System.out.println(tenMinBlocks + " blocks"); 5 1 2 3 4 5 6 7 8 9 10 public class TenMinuteBlocks { public void start() { int hours = 2; int minutes = 14; int totalMins, tenMinBlocks; totalMins = hours * 60 + minutes; tenMinBlocks = totalMins / 10; System.out.println(tenMinBlocks + " blocks"); } } The solution IdentiFy sections oF code that needs to be executed repeatedly: Put that code in a method, and call the method whenever the...
View Full Document

Page1 / 4

L07 - CS101 Lecture 7 1 Computer Science 1 1 SS C Lecture 7...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online