Java的类和对象基础

huangapple go评论105阅读模式
英文:

Basics of classes and objects: Java

问题

  1. public class hotel{
  2. private int Roomno;
  3. private String Name;
  4. private int Charges_day;
  5. private int No_of_days;
  6. public hotel(){
  7. Roomno = 0;
  8. Name = "";
  9. Charges_day = 0;
  10. No_of_days = 0;
  11. }
  12. public void Getit(int r, String n, int c, int no){
  13. Roomno = r;
  14. Name = n;
  15. Charges_day = c;
  16. No_of_days = no;
  17. }
  18. public String Showname(){
  19. return Name;
  20. }
  21. public int Showroomno(){
  22. return Roomno;
  23. }
  24. public int ShowCharges(){
  25. return Charges_day;
  26. }
  27. public int ShowNoOfDays(){
  28. return No_of_days;
  29. }
  30. public int Compute(){
  31. return Charges_day * No_of_days;
  32. }
  33. }
英文:

The question that I received:

Define a class hotel with the following specifications:

Private members:

  1. Roomno
  2. Name
  3. Charges_day
  4. No_of_days

Public members:

  1. Getit() -to enter the data members
  2. Showit() to show the data member
  3. Compute() -To calculate and return the total charges as charges_day * No_of_days

and a constructor function to initialize the data members.


The code I wrote:

  1. public class hotel{
  2. private int Roomno;
  3. private String Name;
  4. private int Charges_day;
  5. private int No_of_days;
  6. public hotel(){
  7. Roomno = 0;
  8. Name = "";
  9. Charges_day = 0;
  10. No_of_days = 0;
  11. }
  12. public void Getit(int r, String n, int c, int no){
  13. Roomno = r;
  14. Name = n;
  15. Charges_day = c;
  16. No_of_days = no;
  17. }
  18. public String Showname(){
  19. return Name;
  20. }
  21. public int Showit(){
  22. return Roomno;
  23. return Charges_day;
  24. return No_of_days;
  25. }
  26. public int Compute(){
  27. return (Charges_day*No_of_days);
  28. }
  29. }

I am aware that I can't have more than 1 returns in one method function, but I'm unable to find a way around this.

答案1

得分: 0

关于要求,我建议您只需打印值

  1. public void Showit(){
  2. System.out.println(Name+" "+Roomno+" "+Charges_day+" "+No_of_days);
  3. }

如果您需要一个返回对象的String表示的方法,请重写toString()

  1. public String toString(){
  2. return Name + " " + Roomno + " " + Charges_day + " " + No_of_days;
  3. }

此外,Java 命名的约定是

  • 对于方法和属性使用 lowerCamelCase 表示
英文:

Regarding the requirement, I'd suggest you just print the values

  1. public void Showit(){
  2. System.out.println(Name+" "+Roomno+" "+Charges_day+" "+No_of_days);
  3. }

If you need a method that return a String representation of your object, override toString()

  1. public String toString(){
  2. return Name + " " + Roomno + " " + Charges_day + " " + No_of_days;
  3. }

Also Java convention for naming is

  • lowerCamelCase for methods and attributs

huangapple
  • 本文由 发表于 2020年4月7日 22:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定