简体   繁体   中英

How would I call a method from a different class?

I've been working on this assignment for awhile; a good 1-2 days, and I think I got everything correct for my 1st class that I made for Java. What the assignment want me to do is to make the method and calculation in the first class and then call it within the second class. I made a second class, and I am trying to make the user input the amount of purchased items and in turn, it would show

  • The amount purchased.
  • The discount it showed.
  • The total sum of the purchased.

I've made; on the second class, scanners to hold the amount of numbers entered, and a system.out.println for the amount that the user is going to purchase. But how would I call the method from the other class? I've tried the following . but it just said to make the other method void rather than a double.

Am I doing something wrong?

Here's the code for the 1st class called SoftwareSales.java where I put the calculation and methods into:

UPDATED v2 public class SoftwareSales {

private int unitsSold;
private double UNIT_PRICE;

public SoftwareSales() // Constructor Method
{

    unitsSold = 0;
    UNIT_PRICE = 99.0;

}

public int getUnitsSold() {
    return unitsSold;
}

public void setUnitsSold(int unitsSold) {
    this.unitsSold = unitsSold;
}

public double calculateDiscount() {

    double discount = 0.0;

    if (unitsSold >= 10) {
        discount = 0.0;

    }

    else if (unitsSold < 10) {

        discount = ((double) 20 / 100) * unitsSold;

    }

    else if (unitsSold < 20) {

        discount = ((double) 30 / 100) * unitsSold;

    }

    else if (unitsSold < 50) {

        discount = ((double) 40 / 100) * unitsSold;

    }
    return discount;
}

public double calculateCost() {
    double subTotal = 0.0;
    double total = 0.0;
    double discount = 0.0;

    subTotal = UNIT_PRICE * unitsSold;
    total = subTotal - discount;

    return discount = 0.0;

}

}

Second Code: UPDATED v2 import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    SoftwareSales soldUnits = new SoftwareSales();
    SoftwareSales discount = new SoftwareSales();
    SoftwareSales total = new SoftwareSales();

    int inputUnits;

    System.out.println("Enter amount of software you are purchasing: ");
    inputUnits = sc.nextInt();
    System.out.println("Unit sold: " + soldUnits.getUnitsSold());
    System.out.println("Total discount: " + discount.calculateDiscount());
    System.out.println("Total cost: " + total.calculateCost());

}

}

You must create a new object of the class that you are referencing. You would do this by using the following code in the class Driver

SoftwareSales yourDesiredObjectName = new SoftwareSales();

You can now reference that class by typing out the new class object name you made (for example, System.out.println(yourDesiredObjectName.unitsSold);

In your Driver class, in your main method, you have to create an object of SoftwareSales class. After creating the object of SoftwareSales class now you can call all public methods by using this object.

Example: In your main method:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
                
    String inputString;
    int soldUnits;
    double discount;
    double cost;
        
    System.out.println ("Enter amount of software you are purchasing: ");
    soldUnits = sc.nextInt();
        

    // creating an object of SoftwareSales class
    SoftwareSales softSales = new SoftwareSales();
    // calling method by using object  
    double result = softSales.calculateCost();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM