我需要幫助完成作業。請注意,我已經完成了自己的代碼,但不知道我是否正確地做了,尤其是我家庭作業的最後一句話。對象和屬性值
我的家庭作業:
定義一個叫建立具有以下屬性類。每座建築都有一個平方英尺(面積)和故事。構造函數使用這兩個屬性創建一個Building。方法get_squarefootage(),get_stories(),set_square_footage()和set_stories()將用於獲取和設置各自的屬性值。方法get_info()將返回建築物的所有當前屬性值。 編寫一個程序,讓用戶創建Building對象並更改其屬性值。
package building_hw2;
import java.util.Scanner;
public class Building {
int area;
int stories;
int get_squarefootage() { //get values of the area
return area;
}
int get_stories() { //get values of the stories
return stories;
}
void set_square_footage(int area) { //set values of the area
this.area = area;
}
void set_stories(int stories) { //set values of the stories
this.stories = stories;
}
void get_info() { //return all the current attribute balues of the building
System.out.println("The square footage of the building is " + area);
System.out.println("The building has " + stories + " stories");
}
//main method
public static void main(String[] args) {
Building Bldg = new Building(); //create a building object
Bldg.area = 40000;
Bldg.stories = 5;
Bldg.get_info(); //display the current values of the building
//get user input to create building object
Scanner keybd = new Scanner(System.in);
System.out.println("Please enter the square footage(area) of the building : ");
int bldgArea = keybd.nextInt();
System.out.println("Please enter the stories : ");
int bldgStories = keybd.nextInt();
Bldg.set_square_footage(bldgArea);
Bldg.set_stories(bldgStories);
Bldg.get_squarefootage();
Bldg.get_stories();
Bldg.get_info();
}
}
而你的問題是... –
我是否根據需求編寫代碼?對不起,我知道這聽起來很愚蠢,但它是什麼:) – Insane
我不理解他說「讓用戶創建建築物」的部分 – Insane