Here is my main class for this and I keep getting an error saying that
The abstract class has not been overridden
I have tried making the car class abstract and not overridding, I have tried to override and using an abstract class, without success. I just don't know what I'm doing wrong.
public abstract class Car implements CarbonFootprint {
private double Car;
private double MilesDrivenPerYear;
private double MilesPerGallon;
//Constructor
public Car(double MilesDrivenPerYear, double MilesPerGallon) {
this.MilesDrivenPerYear = MilesDrivenPerYear;
this.MilesPerGallon = MilesPerGallon;
}
//Return miles driven per year
public double getMilesDrivenPerYear() { return MilesDrivenPerYear; }
//Return Miles per Gallon
public double getMilesPerGallon() { return MilesPerGallon; }
public void setMilesDrivenPerYear(double MilesDrivenPerYear) {
this.MilesDrivenPerYear = MilesDrivenPerYear;
}
public void setMilesPerGallon(double MilesPerGallon) {
this.MilesPerGallon = MilesPerGallon;
}
#Override
public String toString() {
return String.format("%s: %n%s: %s", "Car", "Miles
Driven: ",getMilesDrivenPerYear(), "Miles per
Gallon; ",getMilesPerGallon());
}
public abstract double Car();
public double getCarbonFootprint() {
return Car = getMilesDrivenPerYear() / getMilesPerGallon() * 19.82;
}
}
//end car class'
public class CarbonFootprintTest {
public static void main(String[] args) {
ArrayList FootprintList = new ArrayList();
Car Footprint1 = new Car(25, 36);
FootprintList.add(Footprint1);
Building Footprint2 = new Building(78, 78);
FootprintList.add(Footprint2);
Bicycle Footprint3 = new Bicycle(90);
FootprintList.add(Footprint3);
System.out.println("Shaina Carbon Footprint Calculator");
for (Object Footprint: FootprintList) {
System.out.printf("Miles Driven:");
System.out.printf("Car Carbon Footprint",
Footprint2.getCarbonFootprint());
}
}
Car is an Abstract class, so you cannot create an instance of it. You should probably make another class that extends the Car class. See this answer for information:
https://stackoverflow.com/a/30317092/7260643
Related
It seems like PdCar class have been upcast to Car type, and I can't find a way to use methods that are not in Car interface. I'm just curious, is there a way to access readLabel method? Or it's just not possible?
Thanks.
interface Car{
}
public class Parcel5 {
public Car car(String s){
class PdCar implements Car {
private String label;
private PdCar(String whereTo){
label = whereTo;
}
public String readLabel(){ return label; }
}
return new PdCar(s);
}
public static void main(String [] args){
Parcel5 p = new Parcel5();
Car d = p.car("toyota");
}
}
It seems like PdCar class have been upcast to Car type
no.
If you want to access methhod readLabel() you have to declare it in the interface Car.
I can't find a way to use methods that are not in Car interface. I'm just curious, is there a way to access readLabel method? Or it's just not possible? Thanks.
interface Car{
String readLabel()
}
public class Parcel5 {
public Car car(String s){
class PdCar implements Car {
private String label;
private PdCar(String whereTo){
label = whereTo;
}
public String readLabel(){ return label; }
}
return new PdCar(s);
}
public static void main(String [] args){
Parcel5 p = new Parcel5();
Car d = p.car("toyota");
System.out.println(d.readLabel());
}
}
I'm using the Java instanceof but it doesn't seem to be working.
I have three java classes that extend a Hero class.
The Hero.java class:
public abstract class Hero {
protected int health;
public Hero() {
}
}
The other three classes:
public class Archer extends Hero {
public Archer() {
}
}
public class Mage extends Hero {
public Mage() {
}
}
public class Warrior extends Hero {
public Warrior() {
}
}
I have this main class WelcomeScreen.java
public class WelcomeScreen {
private Archer archer;
private Mage mage;
private Warrior warrior;
private Hero hero;
public WelcomeScreen() {
// choose a hero (archer/mage/warrior)
hero = archer;
new Game(hero);
}
public static void main(String args[]) {
new WelcomeScreen();
}
}
that instantiates the Game.java class
public class Game {
public Game(Hero chosenHero) {
if (chosenHero instanceof Mage) {
System.out.println("you selected mage");
} else if (chosenHero instanceof Archer) {
System.out.println("you selected archer");
} else if (chosenHero instanceof Warrior) {
System.out.println("you selected warrior");
} else {
System.out.println("you selected NOTHING");
}
}
}
In Game.java, the code is meant to check whether chosenHero is an object of Archer.java, Warrior.java, or Mage.java, but I result with "you selected NOTHING". Why does instanceof fail to check if I already assigned it to Archer.java in the WelcomeScreen?
Because your constants are null. When you say,
private Archer archer;
it is equivalent to
private Archer archer = null;
Additionally, you have created three fields per instance. I think you wanted to do something like
private static final Hero archer = new Archer();
private static final Hero mage = new Mage();
private static final Hero warrior = new Warrior();
See also What does it mean to “program to an interface”?
Alternative solution: get rid of instanceof as it suggests a brittle rigid design, one that's easily broken. Instead try to use other more OOP-compliant solutions such as inheritance, or if complex, a Visitor Design Pattern.
For example, a simple inheritance structure could look something like:
public class WelcomeScreen {
public WelcomeScreen() {
// choose a hero (archer/mage/warrior)
Hero hero = new Archer();
new Game(hero);
}
public static void main(String args[]) {
new WelcomeScreen();
}
}
abstract class Hero {
protected int health;
// other shared fields such as String name,...
public Hero() {
}
public abstract String getType();
public int getHealth() {
return health;
}
}
class Archer extends Hero {
public static final String TYPE = "Archer";
public Archer() {
}
#Override
public String getType() {
return TYPE;
}
}
class Mage extends Hero {
public static final String TYPE = "Mage";
public Mage() {
}
#Override
public String getType() {
return TYPE;
}
}
class Warrior extends Hero {
public static final String TYPE = "Warrier";
public Warrior() {
}
#Override
public String getType() {
return TYPE;
}
}
class Game {
private Hero hero;
public Game(Hero chosenHero) {
this.hero = chosenHero;
System.out.println("You selected a hero of type " + hero.getType());
}
}
I'm new to java and trying to create a simple code checking the gas usage of a given car with given miles per gallon and gas but every time I try to initialize the variables, it keeps giving me errors. inTank and mpg say that only final is permitted and the constructors can't initialize the variable parameters for some reason. If someone could explain to me why and how to fix this I would be grateful. Happens in the Udacity IDE and Ecclipse.
public class MileagePrinter {
public static void main(String[] args)
{
// your code here
private double inTank;
private double mpg;
public MileagePrinter(double gasInTank, double milesPerGallon) {
inTank = gasInTank;
mpg = milesPerGallon;
}
}
}
Reorder the code... you have class variables and a constructor inside the main method...
it must look like
public class MileagePrinter {
private double inTank;
private double mpg;
public MileagePrinter(double gasInTank, double milesPerGallon) {
inTank = gasInTank;
mpg = milesPerGallon;
}
public static void main(String[] args){
// your code here
}
}
Main method and constructor are different from each other. Main is the starting point of execution of a program whereas constructor is used to create an object. We need to take constructor out of main method here, e.g.
public class MileagePrinter {
private double inTank;
private double mpg;
public MileagePrinter(double gasInTank, double milesPerGallon) {
inTank = gasInTank;
mpg = milesPerGallon;
}
public static void main(String[] args){
MileagePrinter pointer = new MileagePrinter(10d, 100d); //create object using constructor
}
}
Personally I prefer to separate the main in a class Main (for example, the name isn't important) for an organization of this type:
main.java :
public class Main {
public static void main(String[] args) {
// your code here
}
}
MileagePrinter.java :
public class MileagePrinter {
private double inTank;
private double mpg;
public MileagePrinter(double gasInTank, double milesPerGallon) {
inTank = gasInTank;
mpg = milesPerGallon;
}
}
I'm stuck on this assignment. I'm given an abstract Observer class with only 1 constructor in it, a constructor with parameters/arguments. (refer below)
public static void main(String[] args) {
PairOfNumbers numbers1 = new PairOfNumbers();
PairOfNumbers numbers2 = new PairOfNumbers();
SumObserver sum = new SumObserver(numbers1);
ProductObserver prod = new ProductObserver(numbers2);
MultiSubjectObserver m = new MultiSubjectObserver();
m.addSubject(numbers1);
m.addSubject(numbers2);
numbers1.setNumbers(20, 10);
numbers2.setNumbers(-10, 15);
}
class Subject {
private List<Observer> observers=new ArrayList<Observer>();
public void attachObserver(Observer observer) {
this.observers.add(observer);
}
public void detachObserver(Observer observer) {
this.observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer: this.observers)
observer.update(this);
}
}
class PairOfNumbers extends Subject {
private double number1, number2;
public double getNumber1() { return this.number1; }
public double getNumber2() { return this.number2; }
public void setNumbers(double d1, double d2) {
this.number1=d1; this.number2=d2;
this.notifyObservers(); // don't forget to do this!
}
}
abstract class Observer {
public Observer(Subject subject) {
subject.attachObserver(this);
}
abstract public void update(Subject subject);
}
class SumObserver extends Observer {
public SumObserver(PairOfNumbers pair) {
super(pair);
}
public void update(Subject subject) {
PairOfNumbers numbers=(PairOfNumbers)subject;
System.out.println("New sum is: "+(numbers.getNumber1()+numbers.getNumber2()));
}
}
class ProductObserver extends Observer {
public ProductObserver(PairOfNumbers pair) {
super(pair);
}
public void update(Subject subject) {
PairOfNumbers numbers=(PairOfNumbers)subject;
System.out.println("New product is: "+(numbers.getNumber1()*numbers.getNumber2()));
}
}
Okay, now I'm suppose to create another class which inherits from the above class.
class MultiSubjectObserver extends Observer{
public MultiSubjectObserver(PairOfNumbers pair){
super(pair);
}
public void addSubject(PairOfNumbers pair){
pair.attachObserver(this);
}
public void update(Subject subject){
PairOfNumbers numbers=(PairOfNumbers)subject;
System.out.println("MultiSubjectObserver activated with numbers: " + (numbers.getNumber1())+", "+(numbers.getNumber2()));
}
}
Is there a way to create a constructor inside the MSO Class which requires no parameter/argument? For example
public MultiSubjectObserver(){
//enter code here
}
Please guide me on this one. Had been thinking for days. Thanks in advance! :D
The instruction is to: Modify the source code to handle any number of Subject objects per Observer.
Expected output:
New sum is: 30.0
MultiSubjectObserver activated with numbers: 20.0, 10.0
New product is: -150.0
MultiSubjectObserver activated with numbers: -10.0, 15.0
Yes you can do this, create a no-arg child class, but you still must call the arg-needing super constructor within the child constructor.
This:
class Child extends Super {
public Child() {
super(args_are_needed);
}
}
The tricky part would be -- what to pass into the super constructor in this default case? In your case this could be:
public MultiSubjectObserver(){
super(null);
}
Caveat: and this will lead to a NullPointerException when the super's constructor is called, due to the line, subject.attachObserver(this);, so no, you can't do this.
A better solution: make sure that MultiSubjectObserver does not extend from Observer!
Perhaps something like:
class MultiSubjectObserver {
private List<Observer> observerList = new ArrayList<Observer>();
public void addSubject(PairOfNumbers numbers1) {
observerList.add(new InnerObserver(numbers1));
}
private class InnerObserver extends Observer {
public InnerObserver(Subject subject) {
super(subject);
}
#Override
public void update(Subject subject) {
System.out.println("From multi-observer: " + subject);
}
}
}
But for this to work, you'd have to give PairOfNumbers a decent toString method, perhaps,
#Override
public String toString() {
return String.format("[%.4f, %.4f]", number1, number2);
}
Edit
Based on the output:
class MultiSubjectObserver {
private static final String FORMAT_STRING = "MultiSubjectObserver activated with numbers: %.1f, %.1f%n";
private List<Observer> observerList = new ArrayList<Observer>();
public void addSubject(PairOfNumbers numbers1) {
observerList.add(new InnerObserver(numbers1));
}
private class InnerObserver extends Observer {
public InnerObserver(Subject subject) {
super(subject);
}
#Override
public void update(Subject subject) {
System.out.printf(FORMAT_STRING, ((PairOfNumbers)subject).getNumber1(), ((PairOfNumbers)subject).getNumber1());
}
}
}
Although that casting is a bit skanky. I like the toString() version much better.
So I have a car class and a car tester class. Here is the car class:
package main;
public class Car {
private long distance;
private double newDistance;
private double gasAmount;
private double newGasAmount;
// Contrsuctor
Car(){
distance = 0;
}
Car(long newDistance){
distance = newDistance;
}
//Accessor
public long getDistance(){
return distance;
}
public double getGasInTank(){
return gasAmount;
}
//Mutator
public void drive(double distance){
newDistance = distance;
}
public void addGas(double gasAmount){
newGasAmount = gasAmount;
}
}
And here is the problem. In my carTester class, why doesnt myVehicle.drive(); work??
It underlines it in red (netBeans) and says "package myVehicle doesn't exist"
package main;
public class CarTester {
Car myVehicle = new Car();
myVehicle.drive();
double gasLeft = myVehicle.getGasInTank();
}
The compiler will issue this message when you attempt to invoke an operation on an Object in the class block.
You need to use a main method in CarTester. Also you need to supply a double distance value as per your drive method.
public class CarTester {
public final static void main(String[] args) {
Car myVehicle = new Car();
myVehicle.drive(33.2);
...
}
}
run your code in CarTester class inside of the method. for example public final static void main(String[] args) {...}...
e.g.
package main;
public class CarTester {
public final static void main(String[] args) {
Car myVehicle = new Car();
myVehicle.drive();
double gasLeft = myVehicle.getGasInTank();
}
}
I think what the problem is is that you don't have a method in class CarTester. The compiler is complaining that it cannot find a package with the name of myVehicle, because it is trying to interpret the line myVehicle.drive(); as a type. You need to change the class CarTester to something like:
public class CarTester
{
public static void main(string[] args)
{
Car car = new Car();
car.drive(10.0);
double gasLeft = car.getGasInTank();
}
}