#Is there a shorter way or easier way to write this code?

1 messages · Page 1 of 1 (latest)

gentle stag
#

import java.util.Scanner;

public class LabProgram {

// Method to convert feet to steps
public static int feetToSteps(double userFeet) {
return (int)(userFeet / 2.5);
}

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double userFeet = scnr.nextDouble();

  int steps = feetToSteps(userFeet); 
  System.out.println(steps); 

}
}

desert oarBOT
#

<@&987246883653156906> please have a look, thanks.

#
TJ-Bot
Import java.util.Scanner; public class LabProgram { // Method to convert feet to steps public static int feetToSteps(double userFeet) { return (int)(userFeet / 2.5); // Convert feet to s

teps by dividing by 2.5
}

// Main method to test feetToSteps method
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double userFeet;

  userFeet = scnr.nextDouble();
  
  System.out.println(feetToSteps(userFeet));

}
}

gentle stag
#

its a bird , its a plane its superman!

#

im just typing until another goat like you TJ comes along

finite pendant
#

shorter sure, but its just putting things onto one line: System.out.println((int) (new Scanner(System.in).nextDouble() / 2.5));

#

i personally think this is worse though

gentle stag
#

how come?

finite pendant
#

its a lot less clear what is going on

#

why 2.5?

#

you cant reuse the scanner

#

etc

gentle stag
#

ahhh

#

i may not need it, but ill try it out before I say anything

coral nymph
#

I would actually make it longer and introduce a constant to remove the magical 2.5 value.

gentle stag
#

SIMON

coral nymph
#

Also please make sure to use meaningful variable names, scnr is not ideal.

#

View it from a metic user's point of view, what's 2.5? If it was this constant however: STEPS_PER_FEET.

gentle stag
#

wait how you did you make the word look like code on discord

#

i think I wrote a new one gave me one sec

#

okay how this code?

#

import java.util.Scanner;

public class LabProgram {

public static final double STEPS_PER_FEET = 2.5;


public static int feetToSteps(double feetWalked) {
return (int) (feetWalked / STEPS_PER_FEET);
}

public static void main(String[] args) {

  Scanner scanne = new Scanner(System.in);

  System.out.print("Enter the number of feet walked: ");
  double feetWalked = scanne.nextDouble();

  int stepsTaken = feetToSteps(feetWalked);
  System.out.println("Estimated steps taken: " + stepsTaken);

  scanne.close();

}
}

gentle stag
#

so TJ bot hows your day

coral nymph
desert oarBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

gentle stag
#
import java.util.Scanner;

public class LabProgram {

   
   public static final double STEPS_PER_FEET = 2.5;


   public static int feetToSteps(double feetWalked) {
      return (int) (feetWalked / STEPS_PER_FEET);
   }

   public static void main(String[] args) {
     
      Scanner scanne = new Scanner(System.in);

     
      System.out.print("Enter the number of feet walked: ");
      double feetWalked = scanne.nextDouble();
  
      int stepsTaken = feetToSteps(feetWalked);
      System.out.println("Estimated steps taken: " + stepsTaken);

      
      scanne.close();
   }
}
#

thanks armor

coral nymph
#

Why did you make the contant public?

#

And you forgot the cloing backticks.

gentle stag
#

thanks

gentle stag
coral nymph
#

Also your method itself is wrong, you're losing precision since you go from a decimal to a natural number.

#

Yes.

gentle stag
#

should I make it private?

coral nymph
#

Don't make things public that don't need to be so.

gentle stag
#

roger that

#

okay , make sure to put double instead of int, put private and remember the closing backsticks

gentle stag
coral nymph
#

Well the conversion issue that I pointed out. Try it with a few numbers that aren't a multiple of 2.5.

gentle stag
#

alright , and dumb question but do I keep public static void main or change that to private as well

coral nymph
#

Keep it as public since it's the entry point.

gentle stag
#

gotcha thank you

#

also it was scanner not scanne right?

coral nymph
#

Yes.

gentle stag
#

oh shoot I think I missed up , it was saying "The program must define and call a method:
public static int feetToSteps(double userFeet)"

gentle stag
#

okay im on zybook and doesnt want to specific sadly

#

I solve it , i forgot but I needed public before because of that and it didn't want precision

#

here the code I used instead

#

import java.util.Scanner;

public class LabProgram {

public static final double STEPS_PER_FEET = 2.5;

public static int feetToSteps(double userFeet) {
return (int) (userFeet / STEPS_PER_FEET);
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

  double feetWalked = scanner.nextDouble();
  
  
  System.out.println(feetToSteps(feetWalked));

  scanner.close();

}
}