Java Lesson 1-3 in review
Lesson 1 |
|
All Java programs start with the skeleton below:
public class Tester
{
public static void main(String args[])
{
}
}
The first line tells us that this is a
- public [everyone can see it]
- class [a class is an object]
- called Tester [Tester must also be the file name]
System.out.println("Hello world");
Prints Hello World to the screen and moves the next output down to the next line
System.out.print("Hello world");
Prints Hello World but keeps next output on the same line
//Programmer: Kosmo Kramer
/*Date created: Sept 34, 1492
School: Charles Manson High School; Berkley, Ca*/
The // makes everything on the line after it a comment. Comments are notes to the programmer but do not affect the code at all. The /* starts a multi-line comment and the */ ends it; anything in between is considered a comment.
Lesson 2 |
|
There are 3 types of variables we will use all the time in Java (in reality there are more 3) int, double, String.
String - used to store things in quotes….like "Hello world". They can also store numbers, like "3456", but the will be unable to do calculations with them. The code sample below creates a String variable called s then stores the text Hello cruel world.
String - used to store things in quotes….like "Hello world". They can also store numbers, like "3456", but the will be unable to do calculations with them. The code sample below creates a String variable called s then stores the text Hello cruel world.
String s = "Hello cruel world";
System.out.println(s);
int - used to store integers (positive or negative). An integer is a whole number; it can never store decimal numbers. The code sample below creates an int variable called age then store the number 59 in it.
int age = 59;
System.out.println(age);
double - used to store "floating point" numbers (decimal fractions); double means "double precision". The code sample below creates a double called d and then stores -3.14 in it.
double d = -3.14;
System.out.println(d);
When we say something like
double x = 1.6;
we are really doing two things at once. We are declaring x to be of type double and we are initializing x to the value of 1.6. All this can also be done in two lines of code (as shown below) instead of one if desired:
double x; //this declares x to be of type double
x = 1.6; //this initializes x to a value of 1.6
You can store an integer into a double but you CANNOT store a double into an integer
Variable names must begin with a letter (or an underscore character) and cannot contain spaces. The only “punctuation” character permissible inside the name is the underscore (“_”). Variable names cannot be one of the reserved words that are part of the Java language.
Lesson 3 |
|
Concatenation - Adding or combing Strings. Uses the + symbol.
Below are 3 common ways Strings are concatenated
Below are 3 common ways Strings are concatenated
String mm = "Hello";
String nx = "good buddy";
String c = mm + nx;
System.out.println(c); //prints Hellogood buddy, no space between o & g
String mm = "Hello";
String nx = "good buddy";
System.out.println(mm + " " + nx); //prints Hello good buddy
System.out.println("Hello" + " good buddy"); // prints Hello good buddy
To find the length of a String use the length() method. A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.
The following code shows how to use the length method
The following code shows how to use the length method
String theName = "Donald Duck";
int len = theName.length();
System.out.println(len); //prints 11…notice the space gets counted
Many times you will just want a small piece of a String (example: you have your full name stored and you just want to get your last name) to do this you use the substring() method.
The substring() method has 2 different forms.
Form 1 - only one number in the ()
The substring() method has 2 different forms.
Form 1 - only one number in the ()
String myPet = "Sparky the dog";
String smallPart = myPet.substring(4);
System.out.println(smallPart); //prints ky the dog
The various characters in a String are numbered starting on the left with 0. These numbers are called indices. (Notice the spaces are numbered too.) So in the above example 4 is referencing 'k' which in the 5th index.
S p a r k y t h e d o g
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Form 2 - two number in the ()
String myPet = "Sparky the dog";
String smallPart = myPet.substring(4, 12);
System.out.println(smallPart); //prints ky the d
How do we get ky the d? Start at k, the 4th index, as before. Go out to the 12th index, ‘o’ in
this case and pull back one letter. That means the last letter is d.
this case and pull back one letter. That means the last letter is d.
Other useful methods are the toUpperCase() and toLowerCase() methods
String bismark = "Where’s MY car?";
System.out.println( bismark.toLowerCase( ) ); // prints where’s my car?System.out.println( bismark.toUpperCase( ) ); // prints WHERE'S MY CAR?
Review Questions
Lab Review Question 1
Given:
Given:
String name = "Bob";
Use the string name to printout the following message:
Hello Bob!
Lab Review Question 2
Given:
Given:
String name = "George Foreman";
Use the string name and the substring method as shown in lesson 3 to display the output below:
Your first name is George.
Your last name is Foreman.
Lab Review Question 3
Given:
Given:
String A = "Harry Crackers";
String B = "Salty Dog;
Use the strings A and B and the substring method as shown in lesson 3 to display the output below:
Harry Dog
Salty Crackers