Java Basic类使用

时间:2015-10-15 01:30:42

标签: java

作业:

打印#include <iostream> #include <string> #include <sstream> using namespace std; int gradecalculate (int age) { return age-5; } string messagereturn (int age) { if (age < 5) {return "You are too young for school.";} else if (age > 17) {return "You are too old for school.";} else { ostringstream oss; oss << "You should be in grade " << gradecalculate(age); if (age == 17) {oss << "\n" << "Hello Senior!";} return oss.str(); } } int main () { int age; cout << "Enter your age: "; cin >> age; cout << messagereturn (age) << endl; return 0; } 的孩子,应用person1方法,然后重新打印,输出如下文字。用换行符结束每一行。

以下程序的示例输出:

incNumKids()

代码:

Kids: 3
New baby, kids now: 4

// ===== Code from file PersonInfo.java =====
public class PersonInfo {
    private int numKids;

    public void setNumKids(int personsKids) {
        numKids = personsKids;
        return;
    }

    public void incNumKids() {
        numKids = numKids + 1;
        return;
    }

    public int getNumKids() {
        return numKids;
    }
}
// ===== end =====

问题:

我无法增加并包含// ===== Code from file CallPersonInfo.java ===== public class CallPersonInfo { public static void main (String [] args) { PersonInfo person1 = new PersonInfo(); person1.setNumKids(3); /* Your solution goes here */ System.out.println("Kids: " + person1.getNumKids()); System.out.println("New baby, kids now: " + person1.getincNumKids()); return; 方法,并再次打印

2 个答案:

答案 0 :(得分:3)

不要增加println语句中的子节点数,因为它不属于那里 - 它不是返回String的语句,而是一个更改PersonInfo对象状态的方法。在它自己的行上单独完成,然后通过调用getNumKids()打印与之前相同的孩子数量。另外,不要调用根本不存在的方法,getincNumKids() ???没有这样的方法

public class CallPersonInfo {
   public static void main (String [] args) {
     PersonInfo person1 = new PersonInfo();

     person1.setNumKids(3);

     /* Your solution goes here  */
     System.out.println("Kids: " + person1.getNumKids());
     person1.incNumKids();
     System.out.println("Kids: " + person1.getNumKids());
     // return;  // no reason for this

答案 1 :(得分:0)

这里有什么对我的课堂作业有帮助

  /* Your solution goes here  */
  System.out.println("Kids: " + person1.getNumKids());
 person1.incNumKids();
 System.out.println("New baby, kids now: " + person1.getNumKids());

它是我现在正在从事的作业的正确解决方案。希望这对您有所帮助。这里是需要hmwk帮助的偷窥者。 x3