Unit 5 Writing Classes
Unit 5 Writing Classes
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such as drive and brake.
Classes are sets of objects which share common characteristics/ behavior and common properties/ attributes.
public int scoreGuess (String guess){
int count = 0;
//Initializes count at 0
for (int i = 0; i <= secret.length() - guess.length(); i++) {
if (secret.substring(i, i + guess.length()). equals(guess)) {
count++;
}
}
return count * Math.pow(guess.length(), 2);
}
public void addMembers (String[] names, int gradYear) {
for (string n : names) {
MemberInfo newM = new MemberInfo (n, gradYear, true);
memberList.add(newM);
}
}
Creation of Classes: should be done with UpperCamelCase and made with a key word for the class.
class MyPainter {
}
Main Method: Called automatically when a class is ran and creates an object.
class MyPainter {
public static void main (String[] args) {
MyPainter obj = new MyPainter();
}
}
The Keyword "This" helps us to see the properties of a class.
Constructor: Called when the object is created and doesn't return a value because the object is automatically called.
class MyPainter {
int paint1;
int paint2;
public MyPainter (int paint1input, int paint2input) //This is a constructor
Get Methods: obtain the properties of an object beyond the defined class
class MyPainter {
int paint1;
int paint2;
public MyPainter (int paint1input, int paint2input) //This is a constructor
public int getpaint1() {
return this.paint1;
}