Home > Mobile >  How does one set a number of spaces based on user input?
How does one set a number of spaces based on user input?

Time:11-02

The assignment is asking me to create a class named Vehicle that stimulates a car moving along a 40 block stretch of road. Here's more information:

Your class will build a vehicle and keep track of its location on the road. Location values may range from -20 to 20. A location value of 0 represents block 0, a location value of 1 represents block 1, a location value of 2 represents block 2, etc. If the user tries to move the vehicle beyond block 20 or -20, set the location to /- 20 respectively.(I dont understand how to do this part)

Variable

int location - An integer that holds the current block location of the car on the road, with possible values ranging from -20 to 20.

Methods

Vehicle () - Sets location to 0. Vehicle (int loc) - If loc is between -20 and 20 inclusive, sets location to loc. Otherwise, sets location to 0. void forward () - Increments the vehicle forward one block. Do not let the user move past block 20. void backward () - Increments the vehicle backward one block. Do not let the user move past block -20. int getLocation () - Returns an integer representing the block location of the car. String toString () - Returns a String representation showing the vehicle as an @ character, with spaces to show its location. When the vehicle is at location -20 the @ character appears at the start of the String. When the vehicle is at a higher position, one space for each number from -20 to the car's current location appears before the @. For example if the car is at block -10, the method will return " @" (10 spaces then the '@'). If the car is at block 5 the method will return " @" (25 spaces then the '@').

Here's my code so far(this is in my Vehicle.java file) :

public class Vehicle
{
  private int location;
  
  public Vehicle(int loc)
  {
    return location; 
  }
  
  public void forward()
  {
    if (location>=-20 && location <=20) 
    {
      location  ;
    }
  }
  
  public void backward()
  {
    if (location>=-20 && location <=20)
    {
      location--;
    }
  }
  
  public int getLocation()
  {
  return location;   
  } 
  
  public String toString()
  {
    return ""; 
  }
}

Here is the runner file:

import java.util.Scanner;

public class runner_Vehicle
{
  public static void main (String str[]){
    Scanner scan = new Scanner(System.in);
    Vehicle v = new Vehicle ();
    String instruction = "";
    while(!instruction.equals("q")){
      System.out.println(v);
      System.out.println("Location: "   v.getLocation());
      System.out.println("Type \"f\" to move forwards, \"b\" to move backwards, \"n\" for new vehicle, \"q\" to quit.");
      instruction = scan.nextLine();
      if(instruction.equals("f")){
        v.forward();
      }
      else if(instruction.equals("b")){
        v.backward();
      }
      else if(instruction.equals("n")){
        System.out.println("Starting location for new vehicle?");
        int start = scan.nextInt();
        v = new Vehicle(start);
        scan.nextLine();
      }
      else if(!instruction.equals("q")){
        System.out.println("Instruction not recognized.");
      }
    }
  }
}

CodePudding user response:

If the user tries to move the vehicle beyond block 20 or -20, set the location to /- 20 respectively. (I dont understand how to do this part)

Simply check it and cap the value

  public void forward()
  {
     location  ;
     if (location > 20) location = 20;
  }
  
  public void backward()
  {
    location--;
    if (location < 20) location = -20;
  }

Regarding the toString method - Look at the StringBuilder class

StringBuilder sb = new StringBuilder();
for (int i = -20; i <= 20; i  ) {
  if (i == getLocation()) {
    sb.append("@");
  }
  else sb.append(" ");
}
return sb.toString();

CodePudding user response:

First time answering a question, but hope I can be of some help: I've tried to explain what each thing does in places I thought you might have been confused. If you are going to hand this in, please remove the comments.

    class Vehicle {
    
    private int location;
    
    public Vehicle() {
        // Known as the constructor.
        // Creates an instance of the class, meaning it creates the vehicle object when you say
        // Vehicle car = new Vehicle();
        location = 0;
    }
    
    public Vehicle(int loc) {
        // known as a parameterized constructor, which is just a constructor but you give it some default values.
        // It does not return a value, all it does it make the object in question.
        // In this case, it would be a Vehicle object.
        if (loc >= -20 && loc <= 20) {
            location = loc;
        }
    }
    
    public void forward() {
        location  ;
        if (location >= 21) {
            location = -20;
        }
    }
    
    public void backward() {
        location--;
        if (location <= -21) {
            location = 20;
        }
    }
    
    public int getLocation() {
        return location;
    }
    
    public String toString() {
        String output = "";
        for (int i = -20; i < location; i  ) {
            output  = ' ';
        }
        return output   '@';
    }
}

Hopefully that helps!

  •  Tags:  
  • java
  • Related