首页 / JAVA / Java初学者:类和方法
Java初学者:类和方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java初学者:类和方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6688字,纯文字阅读大概需要10分钟。
内容图文
![Java初学者:类和方法](/upload/InfoBanner/zyjiaocheng/749/183aae746f094b98ad5fe0b9c4340835.jpg)
我是菜鸟,Java是我的第一个编程语言.
我有这个问题,我正在努力解决:
Define a class to represent a Sugar Bowl sugar characterized by three
things: total capacity (in grams), available quantity and quantity of
the spoon (how many grams of sugar takes the spoon associated with
it). Set in this class:
- A constructor method to create a sugar bowl.
- A method that allows to know the amount of sugar in the sugar bowl.
- A method that allows to know how much it takes a spoonful of sugar.
- A method which, given a number of scoops, removes the corresponding amount of sugar, returning this value.
- A method to add sugar. If the supplied value exceeds the available space, the sugar bowl should be full, and the remaining amount
returned. In other cases, should return zero.Set the class
RunSugarBowl
as main and play around.
public class SugarBowl {
private int totalCapacity;
private int availableSpace;
private int spoonSize;
private int occupiedSpace = totalCapacity-availableSpace;//is the same as amount of sugar in the bowl.
SugarBowl (int totalCapacity){
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
}
public int spoon(){
return spoonSize;
}
public int occupied(){
return occupiedSpace;
}
public void scoops (int numberOfScoops){
int amountTaken = numberOfScoops * spoonSize;
if (amountTaken<=occupiedSpace){
occupiedSpace=occupiedSpace-amountTaken;
System.out.println(amountTaken);}
else{
System.out.println("There's not that amount of sugar in the sugar bowl. Try less.");}
}
public int addSugar (int addedAmount){
if (addedAmount>availableSpace){
int remainingAmount=addedAmount-availableSpace;
availableSpace=0;
occupiedSpace = totalCapacity-availableSpace;
return remainingAmount;}
else{
availableSpace = availableSpace - addedAmount;
occupiedSpace = totalCapacity-availableSpace;
return 0;}
}
}
我现在的问题是我的one.occupied方法返回0而不是200:
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
one.addSugar(300);
System.out.println("Occupied size is : "+ one.occupied());
}
}
解决方法:
首先,作为提示,添加方法标题非常有用,这样您就可以知道您的方法正在尝试做什么. Aka,如果你看一下你的规格,你的许多方法都要求你“知道多少……”所以这些方法应该返回一个数字,而不是立即打印出来的东西(当我开始编码时我犯了同样的错误).
您正在方法中打印出这些数字(这对于调试很有用,但不是您的成品应该做的事情).您可以返回一个int,然后在RunSugarBowl中打印出该整数(见下文).
我已经为您提供了一个包含所需内容的一般框架,并添加了一些可能对您有所帮助的评论.你开始做得很好.如果您有更多问题,请在评论中提问.
public class SugarBowl {
private int totalCapacity;
private int availableSpace;
private int spoonSize;
private int occupiedSpace;//starts at 0, because there's nothing in the bowl.
/**
* Constructor for the sugar bowl.
* @param totalCapacity The total capacity of the bowl.
*/
public SugarBowl (int totalCapacity){
this.totalCapacity = totalCapacity; //set the totalCapacity for the bowl
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
occupiedSpace = 0;
}
/**
* Shows how much sugar can fit in a spoon.
* @return The size of the spoon
*/
public int spoon(){
return spoonSize;
}
/**
* Returns amount of sugar in the bowl.
* @return The amount of occupied space
*/
public int occupied(){
return occupiedSpace;
}
/**
* Removes the amount of sugar based on the
* number of scoops passed into it.
* @param numberOfScoops The number of scoops
* @return The amount of sugar removed
*/
public int scoops (int numberOfScoops){
int possibleAmountTaken = numberOfScoops * spoonSize;
int actualAmountTaken = 0;
//Think about sugar, even if there is less sugar than the spoon size,
//can a spoon still remove that amount?
//aka the only time 0 sugar should be taken is when there is 0 sugar in the bowl
if (possibleAmountTaken<=occupiedSpace){
actualAmountTaken = possibleAmountTaken;
}
else{
//there may still be sugar, just not enough for every scoop, you still have to remove it
//actualAmountTaken = ???
}
occupiedSpace = occupiedSpace - actualAmountTaken;
//what about availableSpace?
//availableSpace = ???
return actualAmountTaken;
}
/**
* Adds the specified amount of sugar to the bowl.
*
* @param addedAmount The amount of sugar added to the bowl
* @return The overflow amount of sugar or 0 if there was no overflow
*/
public int addSugar (int addedAmount){
int overflow = 0;
if (addedAmount>availableSpace){
overflow = addedAmount-availableSpace;
//your bowl is going to be full, what happens to occupiedSpace and availableSpace?
//availableSpace = ???
//occupiedSpace = ???
}
else{
//overflow is already 0 so you don't have to do anything with it
//update availableSpace and occupiedSpace
//availableSpace = ???
//occupiedSpace = ???
}
return overflow;
}
}
以上为主要例子:
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("Sugar overflow: " + Integer.toString(one.addSugar(300))); //once working correctly should print out 100 for the overflow
System.out.println("Occupied size is : "+ one.occupied());
}
}
UPDATE
您使用this.totalCapacity的原因是由于以下代码行:
public class SugarBowl {
private int totalCapacity; //totalCapacity for this object; aka this.totalCapacity refers to this variable
//..
public SugarBowl (int totalCapacity){ // <-- totalCapacity passed in
this.totalCapacity = totalCapacity; //this.totalCapacity is setting the totalCapacity for this instance of the object to the value passed in
//..
注意你的构造函数是如何在一个名为“totalCapacity”的变量中传递的,但该类也有自己的内部变量,名为totalCapacity.没有“this”关键字的可比代码如下:
public class SugarBowl {
private int bowlTotalCapacity; //totalCapacity for this object
//..
public SugarBowl (int totalCapacity){
bowlTotalCapacity = totalCapacity;
//..
你必须确保在初始化之后,无论你以前使用过totalCapacity,你都要将它改为bowlTotalCapacity.使用this.totalCapacity可以更容易地引用此类中的totalCapacity.在这里查看更多信息:http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html.
从技术上讲,在最初构造对象之后,你实际上并没有再次使用totalCapacity,但是如果你想看到如果你不包含这个部分会发生的怪异,试着去理解下面代码中会发生什么:
public class ThisExample {
private int wrongExample = 0;
private int thisExample = 0;
public ThisExample (int wrongExample, int thisExample){
wrongExample = wrongExample;
this.thisExample = thisExample;
}
public int getThisExample(){
return thisExample;
}
public int getWrongExample(){
return wrongExample;
}
}
运行以下内容可以帮助您更好地理解:
public class ThisExampleMain {
public static void main(String[] args) {
ThisExample ts = new ThisExample(50, 50);
//you want this to be 50 but it ends up being 0:
System.out.println("Wrong: " + ts.getWrongExample());
//this returns the correct answer:
System.out.println("Right: " + ts.getThisExample());
}
}
内容总结
以上是互联网集市为您收集整理的Java初学者:类和方法全部内容,希望文章能够帮你解决Java初学者:类和方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。