如何使用set和get方法来询问和检索我的程序的用户输入?

时间:2015-10-26 05:24:55

标签: java class user-input

我刚接触编码并参加java编码的大学课程。我正在学习如何编写一起工作的不同类,并需要帮助编写使用set和get方法请求用户输入的代码。我的作业要求制作2个类,矩形和房子,占用房子的4个房间的面积和宽度,然后计算所有房间的总面积。我创建并运行了两个类,House是主要的。下面是矩形类的代码:

import java.text.NumberFormat;
import java.util.Scanner;

public class Rectangle
{

    // the instance variables
    private double length, width;

    //gets and processes the user input
    public void getUserInput()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Length: ");
        length = input.nextInt();
        System.out.print("Width: ");
        width = input.nextInt();
    }

    // methods of the rectangle class
    public void setLength(double len)
    {
        length = len;
    }

    public void setWidth(double w)
    {
        width = w;
    }

    public double getLength()
    {
        return length;
    }

    public double getWidth()
    {
        return width;
    }

    public double getArea()
    {
        return length * width;
    }
    }

这是矩形类的代码:

import java.util.Scanner;
import java.text.NumberFormat;

public class House
{
public static void main(String args[])
{
    // display a welcome message
    System.out.println();
    System.out.println("Welcome to the House Total Area Calculator!");
    System.out.println();

    Scanner user_input = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y"))
    {
        /**
        creates 4 instances(objects) of the rectangle class.
        each instance represents a room in a 4 bedroom house
        */
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle();
        Rectangle rect3 = new Rectangle();
        Rectangle rect4 = new Rectangle();

        // get input for length / width of each room from the user 
            //room 1
        System.out.println("Enter length and Width of room 1: ");
        rect1.getUserInput();
        System.out.println();

            //room 2
        System.out.println("Enter length and width of room 2: ");
        rect2.getUserInput();
        System.out.println();

            //room 3
        System.out.println("Enter length and width of room 3: ");
        rect3.getUserInput();
        System.out.println();

            //room 4
        System.out.println("Enter length and width of room 4: ");
        rect4.getUserInput();
        System.out.println();

        double totalArea = rect1.getArea() * rect2.getArea() * rect3.getArea() * rect4.getArea();
        System.out.println("The total area of all rooms in the house is: " + totalArea);

        // ask if user wants to continue or exit the program
        System.out.print("Continue? (y/n): ");
        choice = user_input.next();
        System.out.println();
    }
}
}

程序运行正常,但我的HW赋值要求使用set方法“接受存储在长度和宽度字段中的参数”,并使用get方法“返回存储在length和width字段中的方法以及每个房间的面积“。然而,我看到如果我从我的矩形类中移除set并获取长度和宽度的方法(仅保留getArea方法),程序仍然运行相同而没有任何问题。所以我的问题是如何编写set / get方法的代码,以便在不使用getUserInput方法的情况下从用户检索数据输入(如果可能的话)?

1 个答案:

答案 0 :(得分:0)

好的,所以你的Rectangle类可以这样编码 -

private double length;
private double width;

public double getLength() {
    return length;
}
public void setLength(Scanner scan) {
    System.out.print("Enter length");
    this.length = scan.nextDouble();
}
public double getWidth() {
    return width;
}
public void setWidth(Scanner scan) {
    System.out.print("Enter width");
    this.width = scan.nextDouble();
}

public Double getArea(){
    return length * width;
}

House类中的main()方法可以像这样编码 -

System.out.println("Welcome to the House Total Area Calculator!");
        System.out.println();

        Scanner user_input = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            /**
            creates 4 instances(objects) of the rectangle class.
            each instance represents a room in a 4 bedroom house
            */
            Rectangle r1 = new Rectangle();
            Rectangle r2 = new Rectangle();
            Rectangle r3 = new Rectangle();
            Rectangle r4 = new Rectangle();

            // get input for length / width of each room from the user 
                //room 1
            System.out.println("Enter length and Width of room 1: ");
            r1.setLength(user_input);
            r1.setWidth(user_input);
            System.out.println();

                //room 2
            System.out.println("Enter length and width of room 2: ");
            r2.setLength(user_input);
            r2.setWidth(user_input);
            System.out.println();

                //room 3
            System.out.println("Enter length and width of room 3: ");
            r3.setLength(user_input);
            r3.setWidth(user_input);
            System.out.println();

                //room 4
            System.out.println("Enter length and width of room 4: ");
            r4.setLength(user_input);
            r4.setWidth(user_input);
            System.out.println();

            double totalArea = r1.getArea() * r2.getArea() * r3.getArea() * r4.getArea();
            System.out.println("The total area of all rooms in the house is: " + totalArea);

            // ask if user wants to continue or exit the program
            System.out.print("Continue? (y/n): ");
            choice = user_input.next();
            System.out.println();
        }