I am a beginner to java and I am trying to build a smoking saving calculator of some sorts. I am trying to get used to using objects in java but I do not know how I call a method form a different class within a print statement in the main method. I have tried looking online for an answer but I failed to find one that suits me case.
SMOKING CALC
public class SmokingCalc {
public static void main(String args[]) {
System.out.println("********SMOKING CALCULATOR********");
Calc cost = new Calc(getPackets, getWeek, getCost);
System.out.println("How much is a packet of cigarretes?");
cost.getCost();
System.out.println("How many packets do you smoke a week?");
cost.getWeek();
System.out.println("You would save " + getWeek + "units in one week." );
}
}
Calc
import java.util.Scanner;
public class Calc {
Scanner scan = new Scanner(System.in);
private int packs;
private int weekly;
private int cost;
public Calc(int getPacks, int getCost, int getWeekly) {
int input = scan.nextInt();
int input1 = scan.nextInt();
int input2 = scan.nextInt();
packs = input;
weekly = input1;
cost = input2;
}
public int getPackets() {
return packs;
}
public int getWeek() {
return weekly;
}
public int getCost() {
return cost;
}
public int fourWeeksCost() {
int fourWeek;
fourWeek = 4 * cost;
return fourWeek;
}
public int twelveWeekCost() {
int twelveWeek;
twelveWeek = 12 * cost;
return twelveWeek;
}
public int sixMonthsCost() {
int sixMonths;
sixMonths = 26 * cost;
return sixMonths;
}
public int yearlyCost() {
int yearly;
yearly = 52 * cost;
return yearly;
}
}
I would appreciate any help on how to do this, thanks in advance.
suppose this may be a solution for your problem.
Calc.Java
public class Calc {
private int packs;
private int costPerPack;
private int costPerWeek;
public Calc(int getPacks, int cost ) {
packs = getPacks;
costPerPack = cost;
costPerWeek = getPacks * cost;
}
public int getPackets() {
return packs;
}
public int getCostPerPack() {
return costPerPack;
}
public int getCostPerWeek() {
return costPerWeek;
}
public int fourWeeksCost() {
int fourWeek;
fourWeek = 4 * costPerWeek;
return fourWeek;
}
public int twelveWeekCost() {
int twelveWeek;
twelveWeek = 12 * costPerWeek;
return twelveWeek;
}
public int sixMonthsCost() {
int sixMonths;
sixMonths = 26 * costPerWeek;
return sixMonths;
}
public int yearlyCost() {
int yearly;
yearly = 52 * costPerWeek;
return yearly;
}
}
SmokingCalc.java
public class SmokingCalc {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int costforpack,packs;
System.out.println("********SMOKING CALCULATOR********");
System.out.println("How much is a packet of cigarretes?");
costforpack = scan.nextInt();
System.out.println("How many packets do you smoke a week?");
packs = scan.nextInt();
Calc cost = new Calc(packs, costforpack );
System.out.println("You would save " + cost.getCostPerWeek() + "units in one week." );
}
}
Hope this helps.
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.