CSC II Exam 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Use .matches() method to check if a string "440-02-4534" has a pattern of 3 digits - 2 digits - 4 digits

"440-02-4534".matches("\\d{3}-\\d{2}-\\d{4}"); \\d represents a single digit. \\d{3} represents three digits.

Use the .matches() method to see if a string begins with "Java" and is followed by any zero or more characters.

"Java is fun".matches("Java.*"); "Java is cool".matches("Java.*"); "Java is powerful".matches("Java.*"); They all evaluate to true. Java.* is a regex. It describes a string pattern that begins with Java followed by any zero or more characters.

String method that creates a new String from String "Welcome" that says "WABlcomAB"

"Welcome".replace("e", "AB");

String method that creates a new String from String "Welcome" that says "WAlcomA"

"Welcome".replace('e', 'A');

What is the output of the code in the pic on the back of the card?

(1) Performs Person's tasks (2) Invoke Employee's overloaded constructor (3) Performs Employee's tasks (4) Performs Faculty's tasks

StringBuilder method that adds to the end.

.append()

StringBuilder method that returns how much space is allotted for the StringBuilder.

.capacity()

StringBuilder method that returns the character at specified index.

.charAt()

StringBuilder method that deletes based on the arguments.

.delete()

String method that measures if two Strings have the same content with same capitalization.

.equals()

String method that measures if two Strings have the same content ignoring the capitalization.

.equalsIgnoreCase()

StringBuilder method that inserts character(s) at specified point.

.insert()

String method that returns a new String that replaces as set in the parameters.

.replace("")

StringBuilder method that replaces the character(s) based on the arguments.

.replace()

StringBuilder method that reverses the contents of the StringBuilder.

.reverse()

StringBuilder method that sets a new character at specified index.

.setCharAt()

String method that returns an array of Strings consisting of the substrings split by the delimiter.

.split()

StringBuilder method that converts a StringBuilder into a String.

.toString()

Dynamic binding

A method can be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime.

Boxing

A primitive type can be automatically converted to an object using a wrapper class.

What is single inheritance in Java?

A single class can't have two immediate parent classes. It can have a grandparent class, though.

what is a regular expression (regex)?

A string that describes a pattern for matching a set of strings. You can match, replace, or split a string by specifying a pattern.

What is polymorphism?

A variable of a supertype can refer to a subtype object.

ADT

Abstract Data Type

Constructor chaining

All objects inherit from the defined Java Object class. When constructing a new object of the base class, it constructs an object of the super class first (and so on along the chain of inheritance) before constructing the object. When constructing an object of a subclass, the superclass' constructor is invoked and continues up the superclasses until the last constructor is called.

Can you override a private method defined in a superclass?

An instance method can be overridden only if it is accessible. Thus, a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

Where do you use @Override?

Before overriding method in the subclass.

What can the keyword super do?

Call a superclass constructor. Call a superclass method.

Unboxing

Converting an object to a primitive type. Java will automatically do this too.

Implement a method, genericSuperClass, that accepts Being and prints the name of the Being that is passed in. Implement a method, specificSubClass, that accepts Being. If being is an instanceOf Monster, print out the lore state. If being is an instanceOf FictionalCharacter, print out a state that is only of the FictionalCharacter

Create variables m and f to cast the being to the Monster class and Nancy class (?)

Instantiation

Creating an instance of an object/class.

What is inheritance?

Defining a new class from existing classes; super/sub classes

Encapsulation

Details of implementation are hidden from the user. Data is kept private. Use the visibility modifier private; this protects data and encapsulation makes classes easy to maintain as each part is compartmentalized.

Inheritance chain example

Employee extends Student; Student extends Person. (Employee--Student--Person). (child--parent--grandparent). Therefore, to create an Employee, it must create Student. Before a Student can be created, it must create Person. Before a Person can be created, it must create Object. Then Person is created, then Student is created, then Employee is created. That is inheritance chain.

Generalization vs Inheritance

Generalization is the term that we use to denote abstraction of common properties into a base class in UML. The UML diagram's generalization association is inheritance. When we implement generalization in a programming language, it is often called inheritance instead. Generalization and inheritance are basically the same, the terminology just differs depending on the context where it is being used.

keyword final for a class

If added to class, prevents extending.

keyword final for a method

If added to method header, prevents overriding.

When would you use a StringBuilder rather than a String?

If you need the flexibility to be able to append, insert, or delete characters.

Overriding vs Overloading example

Left pic (Overriding): The method p(double i) in class A overrides the same method defined in class B. Right pic (Overloading): The class A has two overloaded methods: p(double i) and p(int i). The method p(double i) is inherited from class B.

Can a static method be inherited? Can a static method he overridden? How can you invoke the hidden static method?

Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. The hidden static methods can be invoked using the syntax: SuperClassName.staticMethodName.

Can a subclass of Circle access the toString method defined in the GeometricObject class using syntax such as super.super.toString()?

No. This is a syntax error.

Abstraction

Separation of class implementation and the use of the class.

Use method .replaceAll() and regex to replace $ + # in string "a+b$#c" with string "OOO"

String s = "a+b$#c".replaceAll("[$+#]", "OOO"); S.o.pln(s); Output: aOOObOOOOOOc

What is the .equals() method for Object class?

The following example shows the usage of lang.Object.equals() method: public class ObjectDemo { public static void main(String[] args) { // get an integer, which is an object Integer x = new Integer(50); // get a float, which is an object as well Float y = new Float(50f); // check if these are equal,which is // false since they are different class System.out.println("" + x.equals(y)); // check if x is equal with another int 50 System.out.println("" + x.equals(50)); } //end main }//end class ObjectDemo Output: false (but x == y would have the value true) true The java.lang.Object.equals(Object obj) indicates whether some other object is "equal to" this one. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object. The declaration for java.lang.Object.equals() method: public boolean equals(Object obj) Parameters: obj − the reference object with which to compare. Return Value: This method returns true if this object is the same as the obj argument; false otherwise.

What must the overridden method and the overriding method have the same?

The overriding method must have the same signature as the overridden method and same or compatible return type. Compatible means that the overriding method's return type is a subtype of the overridden method's return type.

What is passed in methods?

The reference of the object.

Use method .split() and regex to split a string "Java, C?C#, C++" into an array of strings delimited by punctuation marks and print the array.

The regex [ . , : ; ? ] specifies a pattern that matches the punctuation marks. Each of these characters is a delimiter for splitting the string. Thus, the string is split into "Java, C, C#, and C++", which are stored in the array tokens.

Modify the FictionalCharacter class to inherit from Being. It should be modified to inherit the states of name and planetFrom in Being (constructor and toString methods) //Fictional character class 134 class Nancy{ 135 136 //data 137 private boolean isSober; 138 private double moneyAmount; 139 private String coffeeLevel; 140 private Gun gun; 141 142 //no args constructor 143 public Nancy(){} 144 145 //args constructor 146 public Nancy(boolean isSober, double moneyAmount, String 147 coffeeLevel, Gun gun){ 147 this.isSober = isSober; 148 this.moneyAmount = moneyAmount; 149 this.coffeeLevel = coffeeLevel; 150 this.gun = gun; 151 } 152 156 //setter methods 173 174 //getter methods 191 192 //other methods 219 //toString method 220 public String toString(){ 221 return "\nIs Nancy sober? " 222 + this.isSober + "\nNancy has $" + this.moneyAmount 223 + "\nNancy's coffee is " + this.coffeeLevel + 224 ". Nancy has a " + this.gun; 225 } 226 227 }//end Nancy class

Use keyword extends to inherit from Being. Use keyword super in a new constructor to call superclass Being's constructor. Add Being's constructor's arguments in the parameters of the new constructor. Use @Override in the .toString() method to denote that the annotated method is required to override a method in its superclass. Use super keyword in the return statement of the .toString() method so that any time you invoke the .toString() for the subclass, it will call the superclass's .toString() method as well.

Overloading methods

Using the same method signature as defined in the superclass in the subclass but different arguments.

Overriding methods

Using the same method signature as defined in the superclass in the subclass. @Override toString, equals methods

What is the .matches() method?

Very similar to .equals() method but more powerful. It can follow not only a fixed string, but also a set of strings that follow a pattern.

3 pillars of OOT

abstraction/encapsulation inheritance polymorphism

A ________ is known as an abstract data type.

class

What would a polymorphic call look like in the main method for the following method? public static void displayObject(GeometricObject object){ S.o.pln("Created on " + object.getDateCreated() + ". Color is " + object.getColor()); } Hint: Circle and Rectangle are subclasses of GeometricObject.

displayObject(new Circle(1, "red", false)); or displayObject(new Rectangle(1, 1, "blue", true)); The method displayObject takes a parameter of the GeometricObject type. You can invoke displayObject by passing any instance of GeometricObject (e.g., new Circle(1, "red", false) and new Rectangle (1, 1, "blue", false). An object of a subclass can be used wherever its superclass object is used. This is commonly known as polymorphism (from a Greek word meaning "many forms"). Polymorphism means that a variable of a supertype can refer to a subtype object.

Strings are ___________ so to manipulate them we have to create...

immutable; new Strings.

Instance vs Static variables

instance: dependent on the object ex: radius or getArea static: non-dependent on the object, class related ex: howManyObjects

extends

keyword for inheritance

Where is the memory location of String s1 = "test"

pool

Java has ________ inheritance.

single

Where is the memory location of String s1 = new String("test")

the new object

Regex/matches

varString.matches("[A-Za-z0-9]{6}")


Set pelajaran terkait

CSET English - Literary Terms Part 1

View Set

Chapter 11 Investment Decision Criteria

View Set

In ProgressChapter 3: Project Management

View Set

Health Assessment Test 1 Study Guide Q's

View Set

Economics Chapter 8: The Price Level and Inflation

View Set

Chapter 7: Attempt, Conspiracy, and Soliciation

View Set

Intermediate Macro: Business Cycles: Facts & Theory

View Set