Static and Modeling
> Click or hit Control-Enter to run Example.main above
Midterm Review: String Processing
A common task when working with is extracting data from structured text. For example, consider that you are provided employee records in the following format:
Name:Position:Salary
Here are some examples:
Chuchu Challen:Senior Challen Family Dog:10000
Xyz Challen:Junior Challen Family Cat: 8000
Little Helper:Challen Family Roomba
Midterm Review: String Processing
Note that not every record has all possible fields. (Little Helper works in exchange for free electricity.) In addition, some records may have whitespace around the values, as in the data for "Xyz Challen" above.
Midterm Review: String Processing
Your job is to write a function called getSalary
that, given a record in the
format above passed as a String
, returns that person’s salary as an int
.
If the record does not contain a salary or if the record is null
you should
return -1
.
As a reminder we provided a copy of the Java String
documentation on the quiz
cover page.
You may want to refer to that, and in particular to the split
and trim
functions.
Note that you can pass a String
to split
rather than a regular expression,
and it will split the string at each occurrence of the passed String
.
Midterm Review: String Processing
We have also provided a copy of the Java Integer
documentation on the quiz
cover page.
You should examine the Integer.parseInt
function to convert a
String
into an int
:
int i = Integer.parseInt("10");
However, note that this function will not work if there is whitespace
surrounding the integer.
So you’ll need to remove it.
See the String
hints above.
> Click or hit Control-Enter to run the code above
Midterm Review: GCD
Define and implement a function below called gcd
that returns the greatest
common denominator (GCD) as an int
of an array of passed positive int
values.
The greatest common divisor (GCD) of two or more integers, which are not all
zero, is the largest positive integer that divides each of the integers.
The passed array may be null
, empty, or contain only 0s—in these cases return -1
.
Midterm Review: GCD
You do not need to implement a sophisticated GCD algorithm.
A simple iterative solution will work fine.
Once you have dealt with the bad inputs (null
, empty) you may want to try to
implement the following algorithm:
-
First, find the maximum value over the passed array. This is also the place to see if you have been given all zeroes.
-
Now, start at that value and work downward.
-
For each potential GCD, find the remainder when you divide every element in the input array by the potential GCD. If they are all zero, you have found the GCD. If any is non-zero, move on to the next potential value.
> Click or hit Control-Enter to run the code above
Midterm Questions?
Reminder: Quiz 4
The static
Keyword
public class Course {
public static int count = 0;
public static void printName() {
System.out.println("Name");
}
}
public class Example {
public static void main(String[] unused) {
// We can call printName without creating an instance
Course.printName();
// We can increment count without creating an instance
Course.count++;
}
}
static
methods and variables belong to the class, not to a specific
instance.
static
Methods
public class Course {
public static void printName() {
System.out.println("Name");
}
}
public class Example {
public static void main(String[] unused) {
// This works
Course.printName();
// This also works
Course CS125 = new Course();
CS125.printName();
}
}
-
static
methods are called directly on the class, rather than on an instance -
…but they can be called on the instance as well.
static
Methods and this
public class Course {
public String name;
public static void printName() {
// This doesn't work
System.out.println(this.name);
}
}
static
methods can be called without an instance, and so can’t use this
static
Variables
public class Course {
public static int count = 0;
public void printCount() {
System.out.println(count);
}
}
public class Example {
public static void main(String[] unused) {
Course CS125 = new Course();
Course CS225 = new Course();
Course.count++;
CS125.printCount();
CS225.printCount();
}
}
static
variables are shared by all instances of a given class
> Click or hit Control-Enter to run Example.main above
static
, public
, and private
public
and private
also work on static variables and methods
-
public
: the (static) variable can be read or written by anyone -
private
: the (static) variable can only read or written by methods defined on that class -
public
: the (static) method can be called by anyone -
private
: the (static) method can only be called by other methods on that class
static
Object Creation Method
We can also use a class method to be able to return null
when creating a new
object if invalid parameters are supplied.
> Click or hit Control-Enter to run Example.main above