Java的类和对象基础

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

Basics of classes and objects: Java

问题

public class hotel{
    private int Roomno;
    private String Name; 
    private int Charges_day; 
    private int No_of_days;
    
    public hotel(){
        Roomno = 0; 
        Name = "";
        Charges_day = 0;
        No_of_days = 0; 
    }
    
    public void Getit(int r, String n, int c, int no){
        Roomno = r; 
        Name = n;
        Charges_day = c;
        No_of_days = no;
    }
    
    public String Showname(){
        return Name;
    }
    
    public int Showroomno(){
        return Roomno;
    }
    
    public int ShowCharges(){
        return Charges_day;
    }
    
    public int ShowNoOfDays(){
        return No_of_days;
    }
    
    public int Compute(){
        return Charges_day * No_of_days; 
    }
}
英文:

The question that I received:

Define a class hotel with the following specifications:

Private members:

Roomno            
Name
Charges_day     
No_of_days     

Public members:

Getit()      -to enter the data members
Showit()  to show the data member
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:

public class hotel{
    private int Roomno;
    private String Name; 
    private int Charges_day; 
    private int No_of_days;
    
    public hotel(){
        Roomno = 0; 
        Name = "";
        Charges_day = 0;
        No_of_days = 0; 
    }
    
    public void Getit(int r, String n, int c, int no){
        Roomno = r; 
        Name = n;
        Charges_day = c;
        No_of_days = no;
    }
    
    public String Showname(){
        return Name;
    }
    public int  Showit(){
        return Roomno;
        return Charges_day;
        return No_of_days;
    }
    
    public int Compute(){
        return (Charges_day*No_of_days); 
    }
    
}

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

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

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

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

public String toString(){
    return Name + " " + Roomno + " " + Charges_day + " " + No_of_days;
}

此外,Java 命名的约定是

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

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

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

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

public String toString(){
    return Name + " " + Roomno + " " + Charges_day + " " + No_of_days;
}

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:

确定