shitchell

Java Template

Feb 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. public class MyClass
  2. {
  3.     // Variable declarations
  4.     Scanner input;
  5.     String someString;
  6.     int someInt;
  7.     // etc...
  8.    
  9.     public static void main(String[] args)
  10.     {
  11.         // This is where you put code that will run when you run your application
  12.         MyClass mc = new MyClass();
  13.        
  14.         // This will run our "getInt()" method defined below
  15.         // If you look at the method, you'll see that it prompts
  16.         // the user to enter an integer, then sets "someInt" to whatever they typed
  17.         mc.setInt();
  18.        
  19.         // And this calls setString defined below, which sets "someString"
  20.         mc.setString()
  21.        
  22.         // And then we can print the value of those variables now that we've prompted the user to change them
  23.         System.out.println("You entered: " + mc.someInt);
  24.         System.out.println("And then entered: " + mc.someString);
  25.     }
  26.    
  27.     // You'll typically have a constructor here
  28.     // A constructor is where you'll set default values for your variables (which we declared at the top of the file)
  29.     public MyClass()
  30.     {
  31.         input = new Scanner(System.in);
  32.         someString = "";
  33.         someInt = 100;
  34.     }
  35.    
  36.     // And then you can create different helper methods here
  37.     public void setInt()
  38.     {
  39.         System.out.println("Enter an int");
  40.         someInt = input.nextInt();
  41.     }
  42.    
  43.     public void setString()
  44.     {
  45.         System.out.println("Enter a string");
  46.         someString = input.nextString();
  47.     }
  48. }
Add Comment
Please, Sign In to add comment