0

私は Java にかなり慣れていないので、私のコードは非常に見苦しく基本的なものであると確信しています。コンストラクター、セッター、およびゲッターの使用方法を理解しようとしています。このサイトで他の質問を見たり、ビデオを見たり、本を読んだりしてみました。私には理解するのが難しいだけです。誰でも私を助けることができますか?

以下は完全な私のコードです。プロジェクトでセッターとゲッターを使用する必要があり、それらを十分に理解したいと思っています。

import javax.swing.JOptionPane;

public class userCar {

  private int carYear;
  private int speed; 
  private String make;

  public void setCarYear(int carYear){
    this.carYear=carYear;
  }

  public void setCarSpeed(int speed){
    this.speed=speed;
  }

  public void setCarMake(String make){
    this.make=make;
  }

  public int getCarYear(){
    return this.carYear;    
  }

  public int getCarSpeed(){
    return this.speed;    
  }

  public String getCarMake(){
    return this.make;   
  }



  public static void main (String [] args){

    userCar ourCar = new userCar();

    String userCarInput []= new String[3];

    userCarInput= carInfo();

    //THIS IS AN ERROR---
    //"Cannot make a static reference to the non-static method setCarYear(int) from the type  
    //userCar"

    userCar.setCarYear(Integer.parseInt(userCarInput[0]));
    userCar.setCarSpeed(Integer.parseInt(userCarInput[2]));
    userCar.setCarMake(userCarInput[1]);


    //This would be displaying the the car make, year, and speed (using the get methods) but I am 
    //currently testing to make sure the values are correct. The array works fine, but not the    
    //setter/getter 

    JOptionPane.showMessageDialog(null,ourCar.getCarMake()+".\n"+ userCarInput[1]+".\n" + 
        userCarInput[2] +".\n");

  }


  public static String[] carInfo(){
    String stringSpeed, stringYear, stringMake ;

    String carInformation[] = new String[3];


    stringSpeed=JOptionPane.showInputDialog("What is the speed of the car in MPH?");

    //Deals with blank or "bad" entries. Follows in stringYear and stringMake as well.
    if (stringSpeed == null) {
      System.exit(0); }
    while (stringSpeed.trim().length()== 0 || Integer.parseInt(stringSpeed) < 0){
      stringSpeed= JOptionPane.showInputDialog("You did not enter a valid value.\n" +
          "Please enter a valid value for speed.");
      if (stringSpeed == null){
        System.exit(0);} 
    }//ends the while


    stringMake=JOptionPane.showInputDialog("What is the make of the car? [Toyota, "
        + "Cadillac, etc.] ");

    if (stringMake == null) {
      System.exit(0); }
    while (stringMake.trim().length()== 0){
      stringSpeed= JOptionPane.showInputDialog("You did not enter a valid value.\n" +
          "Please enter a valid value for the car's make.");
      if (stringSpeed == null){
        System.exit(0);} 
    }//ends the while


    stringYear=JOptionPane.showInputDialog("What year was the car made?");

    if (stringYear == null) {
      System.exit(0); }
    while (stringYear.trim().length()== 0 || Integer.parseInt(stringYear) < 1769 ||
        Integer.parseInt(stringYear) > 2016){
      stringYear= JOptionPane.showInputDialog("You entered "+ stringYear + ".\n" +
          "The first car was made in 1769, and the latest model is a 2015.\n"
          +"Please enter a valid value for the car's year.");
      if (stringYear == null){
        System.exit(0);} 
    }//ends the while

    carInformation [0] = stringSpeed;
    carInformation [1] = stringMake;
    carInformation [2] = stringYear;

    return carInformation;
  }


}
4

1 に答える 1