Andreas Schleicher presents at the launch of What does child empowerment mean...
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
1. BasicPizza.java
public class BasicPizza {
//Declaring instance variables
String type;
String crust;
String ingredients;
double cost;
//Default Constructor
public BasicPizza() {
super();
}
//Parameterized constructor
public BasicPizza(String type) {
super();
this.type = type;
}
//Setters and Getters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCrust() {
return crust;
}
public void setCrust(String crust) {
this.crust = crust;
}
public String getIngredients() {
return ingredients;
}
2. public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public double getCost() {
return cost;
}
public void setCost() {
this.cost = 5;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
return "BasicPizza [type=" + type + ", crust=" + crust + ", ingredients="
+ ingredients + ", cost=" + cost + "]";
}
}
________________________________________________________________
LiFiCheese.java
public class LiFiCheese extends BasicPizza{
//Declaring instance variables
private String crust;
private double cost;
private String ingredients;
//Default constructor
public LiFiCheese() {
super("Cheese");
this.cost=5;
}
//Setters and getters
public void setCrust()
{
this.crust="thin";
3. }
public String getCrust()
{
return crust;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
System.out.println("You Ordered :");
System.out.println(getType());
setCrust();
System.out.println(getCrust());
System.out.println("Total Cost of :"+getCost());
return "";
}
}
__________________________________________________________________________
LiFiPizza.java
4. import java.util.Scanner;
public class LiFiPizza extends BasicPizza {
//Declaring instance variables
private String type;
private double cost;
private String crust;
private String ingredients;
//Default constructor
public LiFiPizza()
{
super();
this.type = "Meat";
this.cost=5;
}
//Setters and getters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getCost() {
return cost;
}
public void setCost() {
this.cost = this.cost+2;
}
public String getCrust() {
return crust;
}
public void setCrust(String crust) {
if(crust.equals("Thin"))
{
this.crust="Thin";
5. }
else if(crust.equals("Thick"))
{
this.crust="Thick";
}
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
System.out.println("You Ordered :");
System.out.println(getType());
System.out.println(getIngredients()+"<+$2.00>");
System.out.println(getCrust());
System.out.println("Total Cost of :"+getCost());
return "";
}
}
________________________________________________________________________
LiFiUnit5Ch14.java
import java.util.Scanner;
public class LiFiUnit5Ch14 {
public static void main(String[] args) {
//Creating the reference of BasicPizza
BasicPizza bp=null;
//Declaring variables
String typeOfPizza;
6. //Scanner object is used to get the inputs entered by the user.
Scanner sc=new Scanner(System.in);
System.out.print("What type of pizza would you like :");
typeOfPizza=sc.next();
//If the user enters Meat then the if block will be executed
if(typeOfPizza.equalsIgnoreCase("Meat"))
{
//Creating the LiFiPizza Class Object
bp=new LiFiPizza();
//Getting the users choice whether it is Thin or Thick Crust
System.out.print("Thin or Thick Crust:");
String crust=sc.next();
//Calling he setter method by passing the "Thin" or "Thick" crust
bp.setCrust(crust);
//Which Extra Ingredient user wants will be read
System.out.print("What ingredient, sorry, only 1 :");
String ingredient=sc.next();
//Calling the SetIngredients method by passing the ingredient as input.
bp.setIngredients(ingredient);
bp.setCost();
bp.toString();
}
//If the user enters "cheese" then else-if block will be executed.
else if(typeOfPizza.equalsIgnoreCase("Cheese"))
{
bp=new LiFiCheese();
bp.toString();
}
7. }
}
____________________________________________________________________________
Output1:
What type of pizza would you like :Cheese
You Ordered :
Cheese
thin
Total Cost of :5.0
_______________________________________________________________________
Output2:
What type of pizza would you like :Meat
Thin or Thick Crust:Thin
What ingredient, sorry, only 1 :Pepperoni
You Ordered :
Meat
Pepperoni<+$2.00>
Thin
Total Cost of :7.0
____________________________________________________________________
Output3:
What type of pizza would you like :Meat
Thin or Thick Crust:Thick
What ingredient, sorry, only 1 :Sausage
You Ordered :
Meat
Sausage<+$2.00>
Thick
Total Cost of :7.0
____________________________________________________________________
Solution
BasicPizza.java
8. public class BasicPizza {
//Declaring instance variables
String type;
String crust;
String ingredients;
double cost;
//Default Constructor
public BasicPizza() {
super();
}
//Parameterized constructor
public BasicPizza(String type) {
super();
this.type = type;
}
//Setters and Getters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCrust() {
return crust;
}
public void setCrust(String crust) {
this.crust = crust;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
9. }
public double getCost() {
return cost;
}
public void setCost() {
this.cost = 5;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
return "BasicPizza [type=" + type + ", crust=" + crust + ", ingredients="
+ ingredients + ", cost=" + cost + "]";
}
}
________________________________________________________________
LiFiCheese.java
public class LiFiCheese extends BasicPizza{
//Declaring instance variables
private String crust;
private double cost;
private String ingredients;
//Default constructor
public LiFiCheese() {
super("Cheese");
this.cost=5;
}
//Setters and getters
public void setCrust()
{
this.crust="thin";
}
public String getCrust()
10. {
return crust;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
System.out.println("You Ordered :");
System.out.println(getType());
setCrust();
System.out.println(getCrust());
System.out.println("Total Cost of :"+getCost());
return "";
}
}
__________________________________________________________________________
LiFiPizza.java
import java.util.Scanner;
public class LiFiPizza extends BasicPizza {
11. //Declaring instance variables
private String type;
private double cost;
private String crust;
private String ingredients;
//Default constructor
public LiFiPizza()
{
super();
this.type = "Meat";
this.cost=5;
}
//Setters and getters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getCost() {
return cost;
}
public void setCost() {
this.cost = this.cost+2;
}
public String getCrust() {
return crust;
}
public void setCrust(String crust) {
if(crust.equals("Thin"))
{
this.crust="Thin";
}
else if(crust.equals("Thick"))
12. {
this.crust="Thick";
}
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
//toString() method is used to display the contents of the Object.inside it.
@Override
public String toString() {
System.out.println("You Ordered :");
System.out.println(getType());
System.out.println(getIngredients()+"<+$2.00>");
System.out.println(getCrust());
System.out.println("Total Cost of :"+getCost());
return "";
}
}
________________________________________________________________________
LiFiUnit5Ch14.java
import java.util.Scanner;
public class LiFiUnit5Ch14 {
public static void main(String[] args) {
//Creating the reference of BasicPizza
BasicPizza bp=null;
//Declaring variables
String typeOfPizza;
//Scanner object is used to get the inputs entered by the user.
Scanner sc=new Scanner(System.in);
13. System.out.print("What type of pizza would you like :");
typeOfPizza=sc.next();
//If the user enters Meat then the if block will be executed
if(typeOfPizza.equalsIgnoreCase("Meat"))
{
//Creating the LiFiPizza Class Object
bp=new LiFiPizza();
//Getting the users choice whether it is Thin or Thick Crust
System.out.print("Thin or Thick Crust:");
String crust=sc.next();
//Calling he setter method by passing the "Thin" or "Thick" crust
bp.setCrust(crust);
//Which Extra Ingredient user wants will be read
System.out.print("What ingredient, sorry, only 1 :");
String ingredient=sc.next();
//Calling the SetIngredients method by passing the ingredient as input.
bp.setIngredients(ingredient);
bp.setCost();
bp.toString();
}
//If the user enters "cheese" then else-if block will be executed.
else if(typeOfPizza.equalsIgnoreCase("Cheese"))
{
bp=new LiFiCheese();
bp.toString();
}
}
14. }
____________________________________________________________________________
Output1:
What type of pizza would you like :Cheese
You Ordered :
Cheese
thin
Total Cost of :5.0
_______________________________________________________________________
Output2:
What type of pizza would you like :Meat
Thin or Thick Crust:Thin
What ingredient, sorry, only 1 :Pepperoni
You Ordered :
Meat
Pepperoni<+$2.00>
Thin
Total Cost of :7.0
____________________________________________________________________
Output3:
What type of pizza would you like :Meat
Thin or Thick Crust:Thick
What ingredient, sorry, only 1 :Sausage
You Ordered :
Meat
Sausage<+$2.00>
Thick
Total Cost of :7.0
____________________________________________________________________