Java Identifier Not Working - java

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();
}
}

Related

How to avoid an 'Interface Abstract class error'?

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

Access a method of a class defined within a method

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());
}
}

How to call two methods in same class?

How can I call two methods from the same class over one object? I mean I try to write a class and its methods to run above code:
volume = Calculate.do_calc().get_volume(a);
I am creating Calculate class and two methods of it. do_calc() and get_volume(a). How should I write this class to run that code.
Unless do_calc() returns the class in where de function get_volume() is located this should never be done.
Here is a little sample for you.
public class ChainTest {
public static void main(String[] args) {
System.out.println(new ChainTest().do_calc().get_volume(1));
}
public ChainTest do_calc() {
// do something;
return ChainTest.this;
}
public int get_volume(int a) {
return a;
}
}
You don't need to write the code in one line. You can call same object methods in different lines.
Calculator calculator = new Calculator();
calculator.do_calc();
calculator.get_volume(a);
In case, if you want static methods
Calculator.do_calc();
Calculator.get_volume(a);
Case 1 If do_calc is static
public class Calculator {
public static void main(String[] args) {
System.out.println(Calculator.do_calc().get_volume(1));
}
public static Calculator do_calc() {
Calculator calculator = new Calculator();
// do something;
return calculator;
}
public float get_volume(int a) {
return a;
}
}
Case 2 : If do_calc is not static
public class Calculator {
public static void main(String[] args) {
System.out.println(new Calculator().do_calc().get_volume(1));
}
public Calculator do_calc() {
Calculator calculator = new Calculator();
// do something;
return calculator;
}
public float get_volume(int a) {
return a;
}
}
Case 3 : If both have return type float as you mentioned in comment
public class Calculator {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.do_calc();
System.out.println(calculator.get_volume(1));
}
public float do_calc() {
// do something;
return 1f; // return your result
}
public float get_volume(int a) {
// do something;
return a;
}
}
You must return this; at the end of each method of your class if they are not static. If the methods are static, do it like this:
public class Calculation {
public static Calculation do_calc () {
//do your calculation
return null;
}
public static Calculation get_volume(int x) {
//do your calculation
return null;
}
}
Then you can write:
Calculation.do_calc().get_volume(1);
No problem in returning null, as the methods are static and not related to a specific instance of the class. If you don't like it, then return new Calculation();
[Edit]
The first method should return a real object if you need to pass its result to the second method:
public class Calculation {
int result;
public static Calculation do_calc () {
//do your calculation
Calculation c=new Calculation();
c.result = theResultOfTheCalculation;
return c;
}
public void get_volume(int x) {
//do your calculation for example:
System.out.println(result + x);
}
}

Having problems with constructor

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;
}
}

non static variable referenced from static context

I tried making a simple class in java using netbeans IDE. Whenever I try to do execute this it gives such warning."non static variable referenced from static context".Can anyone tell me why it happens and how to solve it. Thanx in advance.
public class HW3Q4 {
class Payment{
private double amount_payment;
public void set_amount(double amount){
amount_payment = amount;
}
public double get_amount(){
return amount_payment;
}
public void paymentDetails(){
System.out.println("The amount of the payment is: "+amount_payment);
}
}
public static void main(String[] args) {
// TODO code application logic here
Payment p1 = new Payment();
p1.set_amount(34000.00);
p1.get_amount();
p1.paymentDetails();
}
}
You make a mistake in creating the object. So this would help you:
public class HW3Q4 {
class Payment{
private double amount_payment;
public void set_amount(double amount){
amount_payment = amount;
}
public double get_amount(){
return amount_payment;
}
public void paymentDetails(){
System.out.println("The amount of the payment is: "+amount_payment);
}
}
public static void main(String[] args) {
// TODO code application logic here
HW3Q4 newInstance = new HW3Q4();
newInstance.init();
}
public void init(){
Payment p1 = new Payment();
p1.set_amount(34000.00);
p1.get_amount();
p1.paymentDetails();
}
}
Your payment class is within the HW3Q4 which try to act similar to say a string field within your class HW3Q4 like private String myString. So in order to avoid the error use:
HW3Q4 h = new HW3Q4 ();
Payment p1 = h.new Payment();
You are declaring a separate Payment class for each instance of HW3Q4. Instead, I think you want to declare Payment in its own file (Payment.java), which would be preferred, but I guess you could declare it as a static inner class - just change class Payment { /* ... */ } to static class Payment { /* ... */ }.

Categories

Resources