Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,96 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/* This demo is for defining and instantiating classes:
* We will model a flight plan which is a document that the dispatcher has with info of the flight, its make,
* model, engine type, routes etc.
*
* We will create an overloaded constructor:
* An overloaded constructor is a method which has the same name with
* the class which activates the use of the class but spawn from the
* default constructor with varying variable types and or variable quantities.
*
*We will look at the order of execution
*
* CLASS DECLARATION
* Access Modifier = public (can be private or protected)
* Keyword = class (to identify the type of java file we are creating, here a class holds an object's
* attributes (variables) and its behavior (methods/functions)
* Name = class name (FlightPlan)
* Class body = {}
*
* */
public class FlightPlan {
// Variable definition [format of declaring a variable = variableType variableName semicolon
String id;
String departure;
String destination;
LocalDateTime departureTime;
List<String> route;

// Constructor Definition
//
/* ANATOMY
* It has keyword private to avoid the class being instantiated elsewhere, this method should just
* execute a command to set up the id, it returns void because there is nothing to be returned.
*
* ConstructorName == same className ideally a method is followed by arguments but,
* This is a noArgumentConstructor which is the default,
// it does not carry any parameters in the ()
* */
private FlightPlan() {
System.out.println("FlightPlan()");
// This initializes the variable [id] to carry randomly generated characters and converts them toString
this.id = UUID.randomUUID().toString();
}

/*Let's add more constructors to initialize the other fields
* 2nd CONSTRUCTOR
* Takes departure & destination (which are in String format) as arguments,
* It will need to generate the id using the [this] keyword to call the first constructor ,
* we check the parameters and initialize the arguments.
*
* */
FlightPlan(String departure, String destination) {
this();
if (departure == null || destination == null) {
throw new IllegalArgumentException();
}
System.out.println(String.format("FlightPlan( %s, %s)", departure, destination));
this.departure = departure;
this.destination = destination;
}

}
//To initialize all the fields
/* This will be the final constructor which will call the 2nd constructor by using the [this] keyword
* and put the parameters (departure and destination) as arguments
*
* */
FlightPlan(String departure, String destination, LocalDateTime departureTime, List<String> route) {
//calls the 2nd constructor so the validation of departure and destination logic is executed first
this(departure, destination);
if (departure == null || route == null) {
throw new IllegalArgumentException();
}
System.out.println(String.format("FlightPlan(%s,%s,%s,%s)",
departure,
destination,
departureTime,
route));
// set the route and time
this.route = route;
this.departureTime = departureTime;
}
// its void, it just prints to the console
public void print(){
String msg = "{" +
"id = '" + this.id + '\'' +
"departure = '" + this.departure + '\'' +
", destination = '" + this.destination + '\'' +
", departureDateTime = '" + this.departureTime + '\'' +
", route = '" + this.route +
'}';
System.out.println(msg);
}
// Now the class is done, we need to add the execution in the entry point which is the main method
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

public class Main {
public static void main(String[] args) {

// Partial flight plan
FlightPlan harareToJohannesburg = new FlightPlan("RGA", "ORT");
// We call the 2nd constructor in class FlightPlan using the name we have given it to call
// the print method
harareToJohannesburg.print();
System.out.println();
// Complete Flight Plan
FlightPlan harareToLondon = new FlightPlan("RGA", "LHR ", LocalDateTime.of(2023,4, 8, 23,15), List.of("RGA","ETA","LHR"));
harareToLondon.print();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

public class Main {
public static void main(String[] args) throws InterruptedException {
/*
Create a radar target
Print the label
Send a change altitude command
Watch the behaviour
*/
// Create radar target
RadarTarget os791 = new RadarTarget(
"OS791",
280,
"B737-800");

// Print current label
System.out.println(os791.getLabel());
System.out.println();

// Change altitude command
os791.changeAltitude(200);
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println(os791.getLabel());
System.out.println();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,60 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RadarTarget {
import static java.lang.Math.random;
import static java.lang.System.out;
import static java.lang.Thread.sleep;
import static java.util.concurrent.Executors.newSingleThreadExecutor;

public class RadarTarget {
private String callSign;
private int flightLevel;
private int requiredFlightLevel;
private String model;

public RadarTarget (String callSign, int flightLevel, String model){
// this will call the instance variable not the constructor parameter
this.callSign= callSign;
this.flightLevel = flightLevel;
this.requiredFlightLevel = flightLevel;
this.model = model;
}
public String getLabel(){
StringBuilder sb = new StringBuilder();
sb.append(this.callSign);
sb.append(System.lineSeparator());
sb.append(this.flightLevel)
.append(this.getClimbDescendIndicator())
.append(this.requiredFlightLevel);
sb.append(model);
return sb.toString();
}
private String getClimbDescendIndicator(){
if (this.requiredFlightLevel > this.flightLevel){
return "Up";
}
if (this.requiredFlightLevel < this.flightLevel){
return "Down";
}
return "=";
}
// Lets tell the pilot to change altitude

public void changeAltitude(int newFL) {
out.println(this.callSign + "change altitude" + newFL);
out.println();

this.requiredFlightLevel = newFL;
// Simulate long-running operation
ExecutorService es = newSingleThreadExecutor();
es.execute(() -> {
try{
sleep((long) (random() * 5 * 1000));
}
catch (InterruptedException e){
e.printStackTrace();
}
this.flightLevel = requiredFlightLevel;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package pluralsight.oop;
//Other that handle other
public class Calculators {
// This class is any other top level class though they are static, they uphold OOP by having
// The class keyword, arguments, constructors and another method that calculates
public static class Distance{
private int x1, x2, y1, y2;

public class Calculators {
public Distance(int x1,int x2, int y1, int y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public double calculate(){
return Math.sqrt((x2 -x1) * (x2 - x1) + (y2-y1) * (y2 -y1));
}
}

}
//Maybe later we may add other calculate like the fuel consumtion we use the Calculator class as staticgit
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package pluralsight.oop;

public final class ConversionHelper {
// Change altitude from feet to flylevel
private static final int METERS_IN_ONE_NM = 1852;
// We can also use this MIONM variable as private static int without the final keyword so that we
// can assign any value to it.
// To define a static block initializer you use the keyword static
/* static {
* metersInOneNm = 1852;
* }
*
* */
public static int fromFeetToFL(int feet){
return feet/100;
}
// Converts Meters in one Nautical Mile, which uses a static variable DECLARED FINAL (constant that takes 1 nautical
// mile to be 1852)
public static double fromNmToMeters(double nm){
return nm * METERS_IN_ONE_NM;
}
}
15 changes: 15 additions & 0 deletions 3-static-members/before/src/main/java/pluralsight/oop/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package pluralsight.oop;
//using the static import
import static java.lang.System.out;
import static pluralsight.oop.ConversionHelper.fromNmToMeters;

public class Main {
public static void main(String[] args) {
int altitudeInFeet = 28000;
double distInNm = 10;
// Call the class name and class methods to call the convertor methods
int altitudeInFL = ConversionHelper.fromFeetToFL(300);
System.out.println(altitudeInFeet + " fr -> FL " + altitudeInFL);

// Let's use a static import for the final conversion calling the method only
double distInMeters = fromNmToMeters(10);
out.println(distInNm + " NM -> " + distInMeters + "m");

// To instantiate the distance class, we cannot use the distance class directly, we have to use
// calculate class then period and the static class name
Calculators.Distance d = new Calculators.Distance(1, 2 ,3,4 );
out.println("Distance is " + d.calculate());
}
}