英文:
How can I display a variable from another class without using getters and setters?
问题
我是一名大一学生,我在这里遇到了很多困难。以下是我必须为我的作业之一做的问题(我可以在StackOverflow上寻求指导)。
创建一个名为Customer
的类,用于确定客户购买信用产品的每月还款金额。该类有五个字段:客户姓名
,联系电话
,产品价格
,月份数量
和每月还款金额
。
为每个字段编写获取和设置方法,除了每月还款金额字段。设置方法必须提示用户输入以下字段的值:客户姓名
,联系电话
,产品价格
和月份数量
。
这个类还需要一个方法来计算每月还款金额(产品价格除以月份数量)。
添加一个名为Finance_Period
的子类,用于确定客户是否需要支付利息。
如果支付产品的月份数大于三个月,则客户需要支付25%的利息,否则不适用利息。
支付产品的最长月份数为12个月。
通过确定客户是否需要支付利息,然后计算每月还款金额,覆盖calculate_repayment()
方法。
创建一个名为Customer_Finance
的类,其中包含测试这两个类的逻辑。
提示用户为第一个对象输入数据,不适用利息,并显示结果;然后提示用户输入适用利息的数据,并显示结果。
我在不使用getter
和setter
的情况下,难以将amtRepay
调用到我的主程序中。
我甚至不确定自己是否正确理解了问题,非常感谢任何指导或建议。
另外,我还有另一个名为Finace_Period
的类,目前什么都没有,我对自己正在做的事情并不完全确定。
这是我的主类,我想在其中显示amtRepay
。
package main;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
//Variables
String name;
int cNumber, months;
double price;
//Input
name = JOptionPane.showInputDialog(null, "Please enter the customer's name:");
cNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the customer's contact number:"));
price = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the price of the product:"));
months = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of repayment months:"));
Customer c = new Customer(name, cNumber, months, price);
JOptionPane.showMessageDialog(null, c.calcAmtRepay());
}
}
这是我的辅助类,其中计算了amtRepay
。
package Main;
public class Customer extends Finance_Period {
//Attributes
private String name;
private int cNumber, months;
private double price, amtRepay;
//Constructors
public Customer(String name, int cNumber, int months, double price) {
this.name = name;
this.cNumber = cNumber;
this.months = months;
this.price = price;
}
//Getters
public String getName() {
return name;
}
public int getcNumber() {
return cNumber;
}
public int getMonths() {
return months;
}
public double getPrice() {
return price;
}
//Setter
public void setName(String name) {
this.name = name;
}
public void setcNumber(int cNumber) {
this.cNumber = cNumber;
}
public void setMonths(int months) {
this.months = months;
}
public void setPrice(double price) {
this.price = price;
}
//Calculation of monthly repayments
public double calcAmtRepay() {
amtRepay = price / months;
return price / months;
}
}
希望这些翻译对您有所帮助。如果您有其他问题,请随时问我。
英文:
I'm a first-year student and I'm really struggling here. This is one of the questions that I have to do for my Assignment (I am allowed to use StackOverflow for guidance).
Create a class named Customer
that will determine the monthly repayment amount due by a customer for a product bought on credit. The class has five fields: customer name
, contact number
, product price
, number of months
, and the monthly repayment amount
.
Write get and set methods for each field, except for the monthly repayment amount field. The set methods must prompt the user to enter the values for the following fields: customer name
, contact number
, product price
, and a number of months
.
This class also needs a method to calculate the monthly repayment amount (product price divided by the number of months).
Add a subclass named Finance_Period
that will determine if a customer will pay interest or not.
If the number of months to pay for the product is greater than three, the customer will pay 25% interest, or else no interest applies.
The maximum number of months to pay for the product is 12.
Override the calculate_repayment()
method by determining if the customer will pay interest or not and calculate the monthly repayment amount.
Create a class called Customer_Finance
that contains the logic to test the two classes.
Prompt the user for data for the first object where no interest applies and display the results; then prompt the user for data where interest is applicable and displays the results.
I'm struggling to call amtRepay
into my main without using getters
and setters
.
I'm not even sure if I understand the question correctly, any guidance or advice would be greatly appreciated.
Also, I have another class called Finace_Period
, there is nothing there yet, I'm not 100% sure of what I'm doing yet.
This is my main class, where I want to display amtRepay
.
package main;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
//Variables
String name;
int cNumber, months;
double price;
//Input
name = JOptionPane.showInputDialog(null, "Please enter the customer's name:");
cNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the customer's contact number:"));
price = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the price of the product:"));
months = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of repayment months:"));
Customer c = new Customer(name, cNumber, months, price);
JOptionPane.showMessageDialog(null, c.calcaAmtRepay());
}
}
and this is my secondary class, where amtRepay
is calculated.
package Main;
public class Customer extends Finance_Period {
//Atributes
private String name;
private int cNumber, months;
private double price, amtRepay;
//Constructors
public Customer (String name, int cNumber, int months, double price) {
this.name = name;
this.cNumber = cNumber;
this.months = months;
this.price = price;
}
//Getters
public String getName() {
return name;
}
public int getcNumber() {
return cNumber;
}
public int getMonths() {
return months;
}
public double getPrice() {
return price;
}
//Setter
public void setName(String name) {
this.name = name;
}
public void setcNumber(int cNumber) {
this.cNumber = cNumber;
}
public void setMonths(int months) {
this.months = months;
}
public void setPrice(double price) {
this.price = price;
}
//Calculation of monthly repayments
public double calcAmtRepay () {
amtRepay = price / months;
return price / months;
}
}
Thanks.
答案1
得分: 1
更改你的构造函数:保留amtRepay参数。
// 构造函数
public Customer(String name, int cNumber, int months, double price) {
this.name = name;
this.cNumber = cNumber;
this.months = months;
this.price = price;
}
请注意,在调用此构造函数之后,amtRepay字段仍然未计算。更好的做法是:在Customer中根本不需要amtRepay字段,因为它的值每次调用calcAmtRepay()
时都会计算,这是一个好主意,否则你还必须在setMonths
和setPrice
中重新计算它。
public double calcAmtRepay() {
return price / months;
}
JOptionPane.showMessageDialog(null, c.calcAmtRepay());
英文:
Change your constructor: leave the amtRepay argument.
//Constructors
public Customer (String name, int cNumber, int months, double price) {
this.name = name;
this.cNumber = cNumber;
this.months = months;
this.price = price;
}
Be aware that after the call to this constructor, the amtRepay field is still not calculated. Better: you don't even need the amtRepay field in Customer as its value is calculated each time you call calcAmtRepay()
which is a good idea because otherwise you have to recalculate it also in setMonths
and setPrice
.
public double calcAmtRepay () {
return price / months;
}
JOptionPane.showMessageDialog(null, c.calcAmtRepay());
答案2
得分: 0
在你的类main.Main
中,你正在引用类main.Customer
,但根据你发布的代码,你只有一个类Main.Customer
。
请将Customer.java
中的package Main;
改为package main;
,然后应该可以正常工作。
编辑:
我没有使用Netbeans,所以我使用了安装了Java扩展的Visual Studio Code。
我创建了一个空目录。在该目录内,我创建了文件build.xml
和子目录src
。在src
内,我创建了包main
并复制了你的代码。我还创建了一个空的类main.Finance_Period
。
我的IDE通知我类main.Customer
有一个名为calcAmtRepay()
的方法,但在类main.Main
中,你调用了calcaAmtRepay()
- 注意calc
和Amt
之间多了一个小写的a
。我修复了这个问题,然后将以下内容添加到我的build.xml
:
<project name="MyProject" default="dist" basedir=".">
<description>
Java assignment build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source">
<!-- Compile the Java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the java-assignment-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/java-assignment-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
然后,我在shell中运行了ant dist
,最后执行了:
java -cp dist/lib/java-assignment-20200901.jar main.Main
来运行该应用程序。
编译和执行都正常运行。
我使用的是OpenJDK 11.0.8 2020-07-14。
如果这些信息中有任何帮助,请告诉我。
编辑2:
我将可工作的代码放在了GitLab上。你可以忽略Dockerfile
和与Docker相关的引用,其他部分我已经解释过了。
英文:
In your class main.Main
you are referring to class main.Customer
, but according to the code you posted you only have class Main.Customer
.
Change package Main;
to package main;
in your Customer.java
and it should work.
Edit:
I don't have Netbeans so I used Visual Studio Code with Java extensions installed.
I created an empty directory. Inside that directory I created file build.xml
and subdirectory src
. Inside src
I created package main
and copied your code. I also created empty class main.Finance_Period
.
My IDE notified me that class main.Customer
has method calcAmtRepay()
, but in class main.Main
you call calcaAmtRepay()
- notice the additional, lowercase a
between calc
and Amt
. I fixed that and then added the following to my build.xml
:
<project name="MyProject" default="dist" basedir=".">
<description>
Java assignment build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source">
<!-- Compile the Java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the java-assignment-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/java-assignment-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Then I ran ant dist
in the shell and finally executed:
java -cp dist/lib/java-assignment-20200901.jar main.Main
to run the application.
Both the compilation and the execution runs fine.
I used OpenJDK 11.0.8 2020-07-14.
Please let me know if any of this helps.
Edit 2:
I put the working code on GitLab. You can ignore the Dockerfile
and references to Docker, the rest I already explained.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论