INFO1113×Week6
Lecture
- Part A: Abstract Classes, Abstract Classes UML, Interfaces Interfaces and UML
- Part B: Default method in Interfaces, Packaging
Abstract Class
- Can define methods and attributes
- Cannot be instantiated
// Declaration
[modifier] abstract class ClassName
// Abstract methods
public static void stack (ClassName element);
Interfaces
- Attributes must be static / constant
- Cannot be instantiated
- Interface is not a class
// Declaration
[modifier] interface InteraceName
- Default method
[modifier] default <returntype> MethodName([parameters])
Subclass can override the default method
Organising
- Classpath: defines a set of dictories exposed to the program
- Packages
package <identifier>[.<nested ident>[...]]
- Java Archives
Tutorial
- Packages:
package my.library
javac -cp .:<directory to .class/.java files> Program.java
java -cp .:<directory to .class files> Program
- Archive
jar -cfm manifest.txt project.jar project/*.class
Challenges
- rounding to 2 decimal place
// Method 1
String toTwoDP = String.format("%.2f",test.cost());
// 1.998 => 2.00
// Method 2 - not always the case
DecimalFormat df = new DecimalFormat("#.##");
String toTwoDP = df.format(test.cost());
// 2.001 => 2
// 2.345 => 2.35
// Method 3
System.out.printf("This trip will cost $%.02f %n",test.cost());