java - 我远程做这件事对吗? Java 方法

标签 java methods

手头的任务:考虑一个类 ratingScore,它表示对某些事物(例如移动)的数字评级。属性:对正在评级的内容的描述、最大可能评级、评级。

它将具有以下方法:从用户处获取评级,返回可能的最大评级,返回评级,返回以适合显示的格式显示评级的字符串。

a.为每个方法写一个方法标题 b.为每个方法编写前置条件和后置条件 C。编写一些java语句来测试该类 d.实现该类。

我想我做了我应该做的事情,但这是一种方法,我不确定我是否有足够的空间来对其进行很大的改变,这就是我到目前为止所拥有的。

import java.util.*;
public class MovieRating 
{
    // instance variables
    private String description = " A movie that shows how racism affect our lives and choices";
    private int maxRating = 10;
    private int rating;

    // methods 

    //precondition: Must have maxRating, rating and description before you post it back to the user.
    //rating between 1 and 10, maxRating is set to 10, description of a movie
    public void writeOutput()
    {
        System.out.println("The max rating is: " + maxRating );
        System.out.println("Your rating is: " + rating );

        System.out.println("The rating for" + description + " is " + rating); 
        System.out.println("while the max rating was " + maxRating);
    }

    // PostCondition: Will write maxRating, rating and description to the user.

    //Precondition: description, enter the rating
    public void readInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What would you rate the movie \"American History x\" out of ten");
        System.out.println(description);
        rating = keyboard.nextInt();
    }
    //postcondition: rating will be set to user's input for the movie American History x.



}

这是我的测试程序..到目前为止还没有太多

public class MovieRatingTester 
{
    public static void main(String[] args)
    {

        //object of the class MovieRating
        MovieRating rating1 = new MovieRating();

        rating1.readInput();

        rating1.writeOutput();

    }

}

那么我是否涵盖了被告知要涵盖的内容?我想我做到了,但我认为我做错了,请告诉我。

最佳答案

好吧,我的观点是:

您的 MovieRating 类(class)缺少 OOP 的一些基本元素,而这正是我认为您应该在本作业中学习的内容。

缺少的第一个元素是构造函数方法,您所做的就是自动为每个新的 MovieRating 分配相同的描述。构造函数的作用是在对象首次构建到系统中时为其赋予唯一的值。

构造函数方法很特殊,它是公共(public)的,并且与类具有完全相同的名称,正如我们所说,在这个方法中,您应该为对象变量赋值。

第二件事是放置 getter/setter,这些方法可以访问您的私有(private)值,并将用于分配/获取它们的值。请注意它们在代码中的使用:

import java.util.*;
public class MovieRating 
{

// instance variables
private String description;
private int maxRating;
private int rating;

/*This is the constructor
  Note the use of .this - the expression is used to call the class form withing  
  itself*/
public MovieRating(String description, int maxRating, int rating) {
    this.setDescription(description);
    this.setMaxRating(maxRating);
    this.setRating(rating);
}

/*These are the getters and setters - get is used for getting the value
  and set is used for assigning a value to it*/
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getMaxRating() {
    return maxRating;
}

public void setMaxRating(int maxRating) {
    this.maxRating = maxRating;
}

public int getRating() {
    return rating;
}

public void setRating(int rating) {
    this.rating = rating;
}

//This is a method for the printing commands - notice the use of the get methods//
public void printRatings()
{
    System.out.println("The max rating is: " + this.getMaxRating() );
    System.out.println("Your rating is: " + this.getRating() );

    System.out.println("The rating for" + this.getDescription() + " is " + 
                        this.getRating()); 
    System.out.println("while the max rating was " + this.getMaxRating();
    }

// PostCondition: Will write maxRating, rating and description to the user.

/*Precondition: description, enter the rating
  Note the use of this.setRating()*/
public void readInput()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What would you rate the movie \"American History x\" out of ten");
    System.out.println(description);
    this.setRating(keyboard.nextInt());
}
//postcondition: rating will be set to user's input for the movie American History x.

}

使用构造函数,您可以创建与测试程序不同的评级

MovieRating rating1 = new MovieRating("description 1", 10, 5);
MovieRating rating2 = new MovieRating("description 2", 9, 7);

关于java - 我远程做这件事对吗? Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775402/

相关文章:

java - 不了解java中序列化的基础知识

java - Java Web 应用程序的可扩展性和性能

java - 检查 JCE Unlimited Strength Jurisdiction Policy 文件

c# - 如何将 "~/default.aspx"转换为 "http://www.website.com/default.aspx"C#?

java - 尝试找出集合中的通用方法签名

java - 每个连接线程模型

java - if 语句中的条件始终为 true

c# - System.Void类型如何为不返回值的方法指定返回值类型?

methods - 将多个参数传递给 react 组件

function - 可以为函数类型创建方法吗?