Home > front end >  Error: Main method not found in class when running eclipse
Error: Main method not found in class when running eclipse

Time:12-31

I am new to coding and I am having a very hard time figuring out what I am doing wrong. when I run the code, I get back:
"Error: Main method not found in class Shipment, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

I am not great at this but I think I need to write an instance method as I can not use static?

I am more just looking to be pointed in the right direction as this is an assignment, but I am pulling my hair out trying to figure out next steps.

Any help would be greatly appreciated.

My assignment details:

  1. Create a Java class named Package that contains the following:

a. Package should have three private instance variables of type double named length, width, and height.

b. Package should have one private instance variable of the type Scanner named input, initialized to System.in.

c. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.

d. Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of same name.

e. Public copy constructor, with a parameter of type Package, used to duplicate an existing Package object.

f. Three public void methods named inputLength, inputWidth, and inputHeight. Each method will prompt the user for the appropriate property, and input a double value using the Scanner object input to initialize the instance variables

g. A public void method named displayDimensions which prints the dimensions as length X width X height (each value separated by a “ X “).

h. A public method of type double named calcVolume that calculates the volume and returns the result as a double value.

Second part of the assignment:

  1. Create a class named Shipment

a. The program must ask for two separate packages to ship

b. The program must calculate the cost difference using the difference in volume: i.e. The base price for a package with volume <=1 is $3, for every unit increase in volume, the cost increases by $1 e.g. 1: a parcel with volume 4, the cost is $3 $1 $1 $1 = $6 e.g. 2: a parcel with volume 2.5, the cost is $3 $1.5 = $4.5

c. Give the following (in order of priority):
i. If there is no difference, display the costs as the same
ii. If the cost of one is less than twice the other, display that it is “slightly more than”
iii. If the cost of one is less than three times the other, display that it is “twice”
iv. If the cost of one is less than four times the other, display that it is “triple”
v. If the cost of one is less than five times the other, display that it is “quadruple”
vi. Otherwise, display that as a calculated multiple (eg 5x, 6x etc)

c. The program must indicate the more costly package (if not same cost) and by how much

d. The program must calculate and display the appropriate message (including proper dimension and cost format)
f. Your code also does NOT need to worry if the user inputs an invalid value for the input (example: invalid length). The output of your code must match the samples.

g. You must change the title i.e. Welcome to shipping calculator!

Here is my code:

import java.util.Scanner;

public class Package {
    
    // Three instance variables – length, width and height (each of type double)
    private double length, width, height;
    // One instance variables – input (type Scanner) initialized to System.in
    private Scanner input = new Scanner(System.in);

    // Default constructor (no-arg) – initialize all three instance variables to 1
    public Package() {
        this.length = this.width = this.height = 1;
    }
        
    
    
    // Initial constructor – initialize all three instance variables
    public Package(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    // Copy constructor – copy Box
    public Package(Package b) {
        this(b.length, b.width, b.height);
    }

    // inputWidth, inputLength, and inputHeight methods that set the instance
    // variables based on user input have not parameters and do not return a value.
    public void inputWidth() {
        this.width = input.nextDouble();
    }

    public void inputLength() {
        this.length = input.nextDouble();
    }

    public void inputHeight() {
        this.height = input.nextDouble();
    }

    // a displayDimensions method that displays the length X Width X height
    // (separated by “X”) and does not return a value.
    public void displayDimensions() {
        System.out.printf("%.2fX%.2fX%.2f%n", length, width, height);
    }

    // a calcVolume method that has no parameters and calculates the volume of the
    // box
    public double calcVolume() {
        return length * width * height;
    }
}


\\ Second class question 2

public class Shipment {
private Package pack1, pack2;
public Shipment() {
}
    public void Welcome() {

    System.out.println("Welcome to Scott Goble's shipping calculator!");
    
    this.pack1 = new Package();
    this.pack2 = new Package();
}// prints the title uses default constructor and 

public void inputPackages() {
    System.out.println("Enter first package dimensions");
    inputPackage(this.pack1);
    System.out.println();
    System.out.println("Enter second package dimensions");
    inputPackage(this.pack2);
}

public void inputPackage(Package pack) {
    pack.inputLength();
    pack.inputWidth();
    pack.inputHeight();
}

public void calculateCost() {
    double volP1 = pack1.calcVolume();
    double volP2 = pack2.calcVolume();
    double costPack1 = 3 (volP1-1);
    double costPack2 = 3 (volP2-1);

System.out.println("Package 1 will cost " costPack1);
System.out.println("Package 2 will cost " costPack2);

if(volP1 == volP2) {
    System.out.println("The shipping costs are the same.");
}else if(volP1 < (2*volP2)){
    double result = volP2 - volP1;
    System.out.println("Package one is "  result  " cheaper than package two");}

}
/* How can I get this if statement to print the cost difference and display which one is more expensive than the other.*/



public void display() {
    System.out.print("First package dimensions: ");
    this.pack1.displayDimensions();
    //add memo about shipping
    
    System.out.print("Second package dimensions: ");
    this.pack2.displayDimensions();}}
    //add memo about shipping

CodePudding user response:

Write the main method for your class

public static void main(String args[]){
}

CodePudding user response:

I hope you are aware of the main method in java?

Why the main method is used in Java?

The main method is used to specify the starting point of the program. This is the starting point of our program from where the JVM starts the execution of the program.

so here in your program just write the main method inside the Shipment class.

package stackoverflow.bingo;

public class Shipment {
    private Package pack1, pack2;

    public Shipment() {
    }

    public static void main(String[] args) {
        Shipment s = new Shipment();
        s.welcome();
    }
    
    public void welcome() {

        System.out.println("Welcome to Scott Goble's shipping calculator!");

        this.pack1 = new Package();
        this.pack2 = new Package();
    }// prints the title uses default constructor and

    public void inputPackages() {
        System.out.println("Enter first package dimensions");
        inputPackage(this.pack1);
        System.out.println();
        System.out.println("Enter second package dimensions");
        inputPackage(this.pack2);
    }

    public void inputPackage(Package pack) {
        pack.inputLength();
        pack.inputWidth();
        pack.inputHeight();
    }

    public void calculateCost() {
        double volP1 = pack1.calcVolume();
        double volP2 = pack2.calcVolume();
        double costPack1 = 3   (volP1 - 1);
        double costPack2 = 3   (volP2 - 1);

        System.out.println("Package 1 will cost "   costPack1);
        System.out.println("Package 2 will cost "   costPack2);

        if (volP1 == volP2) {
            System.out.println("The shipping costs are the same.");
        } else if (volP1 < (2 * volP2)) {
            double result = volP2 - volP1;
            System.out.println("Package one is "   result   " cheaper than package two");
        }

    }
    /*
     * How can I get this if statement to print the cost difference and display
     * which one is more expensive than the other.
     */

    public void display() {
        System.out.print("First package dimensions: ");
        this.pack1.displayDimensions();
        // add memo about shipping

        System.out.print("Second package dimensions: ");
        this.pack2.displayDimensions();
    }
    // add memo about shipping
    
    
}

Learn more about the execution of the java program:execution

  •  Tags:  
  • java
  • Related