如何从另一个类调用get方法?

时间:2014-03-23 13:06:19

标签: java get

在这个程序中,我想计算船舶的燃油消耗量。方法消耗基于计算燃料消耗 坦克的大小和旅行时间;坦克大小,行驶距离和平均速度;坦克的大小和天数的差异。 然后我想通过公共get方法打印所有3个fuelConsumptions。我应该如何在Test类中调用get方法?

船舶:

import java.util.GregorianCalendar;

class Ship {

private double fuelTank;
private double fuelConsumption1;
private double fuelConsumption2;
private double fuelConsumption3;
private double hoursOfTravel;
private double distance;
private double averageSpeed;
private GregorianCalendar dateOfDeparture;
private GregorianCalendar dateOfArrival;

public Ship(double newFuelTank, double newHoursOfTravel, double newDistance, double newAverageSpeed,
        GregorianCalendar dateOfDeparture, GregorianCalendar dateOfArrival) {

    this.fuelTank = newFuelTank;        
    this.hoursOfTravel = newHoursOfTravel;
    this.distance = newDistance;
    this.averageSpeed = newAverageSpeed;
    this.dateOfDeparture = dateOfDeparture;
    this.dateOfArrival = dateOfArrival;
}

public Ship (double fuelConsumption1, double fuelConsumption2, double fuelConsumption3) {

    this.fuelConsumption1 = fuelConsumption1;
    this.fuelConsumption2 = fuelConsumption2;
    this.fuelConsumption3 = fuelConsumption3;
}

public double getFuelTank() {
    return fuelTank;
}

public void setFuelTank(double fuelTank) {
    this.fuelTank = fuelTank;
}

public double getFuelConsumption1() {
    return fuelConsumption1;
}

public void setFuelConsumption1(double fuelConsumption1) {
    this.fuelConsumption1 = fuelConsumption1;
}

public double getFuelConsumption2() {
    return fuelConsumption2;
}

public void setFuelConsumption2(double fuelConsumption2) {
    this.fuelConsumption2 = fuelConsumption2;
}

public double getFuelConsumption3() {
    return fuelConsumption3;
}

public void setFuelConsumption3(double fuelConsumption3) {
    this.fuelConsumption3 = fuelConsumption3;
}

public double getHoursOfTravel() {
    return hoursOfTravel;
}

public void setHoursOfTravel(double hoursOfTravel) {
    this.hoursOfTravel = hoursOfTravel;
}

public double getDistance() {
    return distance;
}

public void setDistance(double distance) {
    this.distance = distance;
}

public double getAverageSpeed() {
    return averageSpeed;
}

public void setAverageSpeed(double averageSpeed) {
    this.averageSpeed = averageSpeed;
}

public GregorianCalendar getDateOfDeparture() {
    return dateOfDeparture;
}

public void setDateOfDeparture(GregorianCalendar dateOfDeparture) {
    this.dateOfDeparture = dateOfDeparture;
}

public GregorianCalendar getDateOfArrival() {
    return dateOfArrival;
}

public void setDateOfArrival(GregorianCalendar dateOfArrival) {
    this.dateOfArrival = dateOfArrival;
}

public double consumption(double fuelConsumption1, double fuelTank, double hoursOfTravel) {
        fuelConsumption1 = fuelTank / hoursOfTravel;
    return fuelConsumption1;
}

public double consumption(double fuelConsumption2, double fuelTank, double distance, double averageSpeed) {
    fuelConsumption2 = fuelTank / (distance / averageSpeed);
    return fuelConsumption2;
}

public double difference_in_MS(GregorianCalendar dateOfDeparture, GregorianCalendar dateOfArrival) {
    double differenceMS = (dateOfDeparture.getTime()).getTime() - (dateOfArrival.getTime()).getTime();
    return differenceMS;
}

public double difference_in_days(double differenceMS, double fuelTank) {
    double differenceDays = differenceMS / (24 * 60 * 60 * 1000); 
    double p = fuelTank / differenceDays;
    return p;
}

public double consumption(double fuelConsumption3, double p) {
    fuelConsumption3 = p;
    return fuelConsumption3;
}

Ship c = new Ship (fuelConsumption1, fuelConsumption2, fuelConsumption3);
}

测试:

import java.util.GregorianCalendar;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the size of the tank:");
        double newFuelTank = sc.nextDouble();

        System.out.println("Enter the number of hours of travel: ");
        double newHoursOfTravel = sc.nextDouble();

        System.out.println("Enter the traveled distance and the average speed of travel: ");
        double newDistance = sc.nextDouble();
        double newAverageSpeed= sc.nextDouble();

        System.out.println("Enter the year of departure of the ship:");
        String yD = sc.next();
        int newYearD = Integer.parseInt(yD);

        System.out.println("Enter the month of departure of the ship:");
        String mD = sc.next();
        int newMonthD = Integer.parseInt(mD) - 1;

        System.out.println("Enter the date of departure of the ship:");
        String dD = sc.next();
        int newDateD = Integer.parseInt(dD);

        System.out.println("Enter the year of arrival of the ship:");
        String yA = sc.next();
        int newYearA = Integer.parseInt(yA);

        System.out.println("Enter the month of arrival of the ship:");
        String mA = sc.next();
        int newMonthA = Integer.parseInt(mA) - 1;

        System.out.println("Enter the date of arrival of the ship:");
        String dA = sc.next();
        int newDateA = Integer.parseInt(dA);


        GregorianCalendar dateOfDeparture = new GregorianCalendar(newYearD, newMonthD, newDateD);

        GregorianCalendar dateOfArrival = new GregorianCalendar(newYearA, newMonthA, newDateA);

        Ship ship1 = new Ship(newFuelTank, newHoursOfTravel, newDistance, newAverageSpeed, dateOfDeparture, dateOfArrival);

        System.out.println("Ship:"
                + "\n" + "- The size of the tank: " + ship1.getFuelTank()
                + "\n" + "- The number of hours of travel: " + ship1.getHoursOfTravel()
                + "\n" + "- The traveled distance: " + ship1.getDistance()
                + "\n" + "- The average speed of travel: " + ship1.getAverageSpeed()
                + "\n" + "- Date of departure: " + ship1.getDateOfDeparture()
                + "\n" + "- Date of arrival: " + ship1.getDateOfArrival()
                + "\n" + "- Fuel consumption: " + c.getFuelConsumption1() + ", " + c.getFuelConsumption2() + ", " + c.getFuelConsumption3());

        sc.close();
    }

}

1 个答案:

答案 0 :(得分:0)

如果您的Ship类与Test类具有相同的包名,那么您可以:

System.out.println("Ship:"
  + "\n" + "- The size of the tank: " + ship1.getFuelTank()
  + "\n" + "- The number of hours of travel: " + ship1.getHoursOfTravel()
  + "\n" + "- The traveled distance: " + ship1.getDistance()
  + "\n" + "- The average speed of travel: " + ship1.getAverageSpeed()
  + "\n" + "- Date of departure: " + ship1.getDateOfDeparture()
  + "\n" + "- Date of arrival: "   + ship1.getDateOfArrival()
  + "\n" + "- Fuel consumption: "  + ship1.c.getFuelConsumption1() + ", " + 
                                     ship1.c.getFuelConsumption2() + ", " + 
                                     ship1.c.getFuelConsumption3());