10){ b = false; } System.out.println(“done with loop”); }"> 10){ b = false; } System.out.println(“done with loop”); }">
Download presentation
Presentation is loading. Please wait.
Published byDomenic Shaw Modified over 8 years ago
1
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class
2
Review ● Program Structure ● System.out.println(“hello!”); ● Variables ● Control – If-then-else – Loops
3
Review public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World from Robot laptop"); int i = 0; boolean b = true; while (b == true){ System.out.println("HelloWorld: " + i); i = i + 1; if(i > 10){ b = false; } System.out.println(“done with loop”); }
4
Incrementing an integer int j = 5; j = j + 1; System.out.println(“value: ” + j); j++; System.out.println(“value: “ + j);
5
The “For” loop for (j = 0; j < 20; j++){ //do some stuff }
6
Task 1 Use a “for” loop – print out “Hello World” 10 times
7
Subroutines ● like a subprogram ● you can call this subprogram from within your program ● you can pass variables to it ● you can return values from it ● allows an important concept called “abstraction”
8
Subroutine Example, Part 1 void main(){ int radius = 5; double area; area = calculateArea(radius); radius = 10; area = calculateArea(radius); }
9
Subroutine Example, Part 2 double calculateArea(int radius){ double area; area = 3.14 * (radius * radius) return area; }
10
Task 2 ● write a subroutine to calculate the circumference of a circle. ● print out the circumference of a circle with a radius of 5 ● print out the circumference of a circle with a radius of 20 ● print out the circumference of a circle with a radius of 23.3;
11
Subroutine Review public class HelloWorld { public static double calculateArea(int radius){ double x; System.out.println("inside calculateArea, radius: " + radius); x = 3.14 * radius * radius; return x; } public static void main(String[] args) { System.out.println("Hello World from Robot laptop"); double sheehyArea = calculateArea(6); System.out.println("my area was: " + sheehyArea); sheehyArea = calculateArea(10); }
12
String ● name some of the java primitive data types ● there is another “type” called “String” ● but..... ● it isn't really a type ● it is an “Object”. Hold that thought!
13
The “String” Class String myName = “Mr. Sheehy”; System.out.println(“my name is “ + myName); ● Each character in the string has an index – starting with 0 and counting up ● ok, so what is the big deal?
14
The String Class ● you can do operations on a String object!! ● String myName = “Mr. Sheehy”; ● System.out.println( “length: “ + myName.length()); ● System.out.println( “uppercase: + myName.toUpperCase());
15
The Java API ● http://docs.oracle.com/javase/6/docs/api/
16
Task 3 ● declare and initialize a String object ● initialize it to your full name ● using the java api – get a new String that is a substring of your name – the new String should be only your middlename
17
For next time ● the String class introduces an important concept..... – Objects and “Object Oriented Programming”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.