Homework 6 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
I have posted homework 6 here.
It is due this coming Sunday at 11:59 PM.
The mid-term exam will be given on Monday, March 23rd.
It will consist of questions like those on the quizzes along with questions asking you to write short segments of Java code.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
The other 40% will come from four questions that ask you to write a short segment of code.
The last class before the exam, Friday, March 13th, will be a review session.
You will only be responsible for the material in the Class Notes for that class on the exam.
The Mid-term is a closed book exam.
The due date for homework assignments 2 - 6 is Friday, March 20th at 11:59 PM.
That is the Friday of the Spring Break.
Any of these assignments submitted after that date will receive a score of 0.
Expression | Read As |
---|---|
x += 2 | add 2 to x |
x -= 2 | subtract 2 from x |
x *= 2 | mulitply x by 2 |
x /= 2 | divide x by 2 |
x %= 2 | take the remainder when x is divided by 2 |
Description | Operators |
---|---|
Unary operators | ! ++ -- |
Multiplicative operators | * / % |
Additive operators | + - |
Relational operators | < > <= >= |
Equality operators | == != |
Logical AND | && |
Logical OR | || |
Assignment operators | = += -= *= |
public class Print3Args { public static void main (String[] args){ System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); } } $ java Print3Args foo bar bletch foo bar bletch
for
loop to get each value in an array
but we have to know the length of the array to do it
ARRAY_VARIABLE.length
args.length
public class PrintArgs {
public static void main (String[] args){
for (int i = 0; i < args.length; i++){
System.out.println(args[i]);
}
}
}
$ java PrintArgs 1 2 3 4 5
1
2
3
4
5
DATA_TYPE int VARIABLE_NAME = new DATA_TYPE[LENGTH];
Type | Value |
---|---|
int | 0 |
double | 0.0 |
char | '\0' |
boolean | false |
{VALUE, VALUE, ...}
VARIABLE_NAME = NEW_VALUE
VARIABLE_NAME = NEW_VALUE[INDEX]
String VARIABLE_NAME = STRING_LITERAL;
String greeting = "Hello";
System.out.println(greeting + "John");
number = Integer.parseInt(input);
public static int rand_int(int max, int min){
double value = Math.random() * (max - min) + min;
return (int) value;
}
static
means an object does not have to be created
to use this method
die_1 = rand_int(7, 1); die_2 = rand_int(7, 1);
int score = Integer.parseInt(input);
CLASS_NAME_OR_OBJECT_NAME.METHOD_NAME(PARAMETER_LIST
int score = Integer.parseInt(input);
String name = "John Smith" System.out.println(name + " is " name.length() + " long");
Method | Description | Return Type |
---|---|---|
charAt(index) | Returns the character at a specific position | char |
contains(text) | Returns true if the string contains the text | boolean |
endsWith(text) | Returns true if the last characters in the string are text | boolean |
equals(string) | Returns true if the two strings are equal | boolean |
length() | Returns the number of characters in the string | int |
replace(text_1, text_2) | Returns a new string where all the occurrences of text_1 are replaced by text_2 | String |
split(text) | Returns an array of strings. The text is used to mark off each string from the next | String [] |
startsWith(test) | Returns true if the the first characters of the string are text | boolean |
substring(start_index, stop_index) | Returns a string starting at the position of start_index and ending just before stop_index | String |
toLowerCase() | Returns a new string will all characters lowercase | String |
toUpperCase() | Returns a new string will all characters UPPERCASE | String |
public class CharAt { public static void main (String[] args){ String name = "Tom"; System.out.println("name " + name); System.out.println("name.charAt(0) " + name.charAt(0)); System.out.println("name.charAt(1) " + name.charAt(1)); System.out.println("name.charAt(2) " + name.charAt(2)); } } $ java CharAt name Tom name.charAt(0) T name.charAt(1) o name.charAt(2) m
public class StringEquals { public static void main (String[] args){ String string_1 = args[0]; String string_2 = args[1]; System.out.println(string_1.equals(string_2)); } } $ java StringEquals foo foo true $ java StringEquals foo bar false
public class StringContains { public static void main (String[] args){ String string = args[0]; String text = args[1]; System.out.println(string.contains(text)); } } $ java StringContains Mississippi pp true $ java StringContains Mississippi ss true $ java StringContains Mississippi q false
public class StringLoop { public static void main (String[] args){ String name = "Tommy"; System.out.println(name); for (int i = 0; i < name.length(); i++){ System.out.println("name.charAt(" + i + ") " + name.charAt(i)); } } } $ java StringLoop Tommy name.charAt(0) T name.charAt(1) o name.charAt(2) m name.charAt(3) m name.charAt(4) y
public class ReplaceString { public static void main (String[] args){ String string = args[0]; String old_text = args[1]; String new_text = args[2]; System.out.println(string.replace(old_text, new_text)); } } $ java ReplaceString Mississippi ss s Misisippi $ java ReplaceString Hoffman,Glenn , " " Hoffman Glenn
"John, Smith, 0124353"
public class SplitString {
public static void main (String[] args){
String string = args[0];
String delimiter = args[1];
String [] fields = string.split(delimiter);
for (int i = 0; i fields.length; i++){
System.out.println(fields[i]);
}
}
}
$ java SplitString "John, Smith, 0124353" ", "
John
Smith
0124353
$ java SplitString "2020-01-10" "-"
2020
01
10
string.length()
fields.length
public class StartsWith { public static void main (String[] args){ String string = args[0]; String text = args[1]; System.out.println(string.startsWith(text)); } } $ java StartsWith Mississippi M true $ java StartsWith Mississippi Mis true $ java StartsWith Mississippi m false $ java StartsWith Mississippi q false
public class EndsWith { public static void main (String[] args){ String string = args[0]; String text = args[1]; System.out.println(string.endsWith(text)); } } $ java EndsWith Mississippi ppi true $ java EndsWith Mississippi pp false
public class Substring { public static void main (String[] args){ String string = args[0]; int start_index = Integer.parseInt(args[1]); int end_index = Integer.parseInt(args[2]); System.out.println(string.substring(start_index, end_index)); } } $ java Substring Mississippi 2 5 ssi $ java Substring Mississippi 0 5 Missi $ java Substring Mississippi 5 11 ssippi
public class ToLowerCase { public static void main (String[] args){ String string = args[0]; System.out.println(string.toLowerCase()); } } $ java ToLowerCase UPPERCASE uppercase $ java ToLowerCase lowercase lowercase $ java ToLowerCase 123 123 $ java ToLowerCase '%$#@' %$#@ $ java ToLowerCase lower123%^UPPER lower123%^upper
public class ToUpperCase { public static void main (String[] args){ String string = args[0]; System.out.println(string.toUpperCase()); } } $ java ToUpperCase lowercase LOWERCASE $ java ToUpperCase UPPERCASE UPPERCASE $ java ToUpperCase 123 123 $ java ToUpperCase %$#@ %0@ $ java ToUpperCase lower123%^upper LOWER123%^UPPER
Space | ' ' |
Tab | '\t' |
Newline | '\n' |
public class Trim { public static void main (String[] args){ String string_1 = " hello "; String string_2 = "\tthere\t"; String string_3 = "\nworld\n"; show_string_boundaries(string_1); show_string_boundaries(string_1.trim()); show_string_boundaries(string_2); show_string_boundaries(string_2.trim()); show_string_boundaries(string_3); show_string_boundaries(string_3.trim()); } public static void show_string_boundaries(String s){ System.out.println("---" + s + "---"); } } $ java Trim --- hello --- ---hello--- --- there --- ---there--- --- world --- ---world---