Saturday, June 28, 2014

First use of Eclipse

Double click the Eclipse icon to start it

You will get this dialog



Your workspace is where all you files will be stored by default. These are the files that will show up in the package explorer. You can change your workspace location if you wish.

When Eclipse opens the first time you will have a screen that shows various options. Click get started to get to the IDE. (I am doing this from memory so it may be a little different.)

From the FILE menu select NEW then Java Project. Give the project a name.



Then click Next to see how the project will be laid out. You could also just click Finish if you prefer.

In the package explorer click the triangle to expand the project.



The src folder is where your files will go. Right Click on the src folder and add new class. You will get the following dialog:



I have filled out the main details. First you should give a package name. The convention is a sort of reverse url: "com.spconger.FirstProject." This gives the com, my user name and the name of the project. You don't have to follow this convention, but it makes assigning package names easier. A package functions much like a namespace in C#. It is used to group classes that belong together and differentiate them from classes in other packages that might have the same name. You have to give the class a name. I chose Program for this one. I usually call the class that contains the main() function program. The convention for class names is to start them with a capital letter and then capitalize the first letter of each succeeding word. Of course there can be no spaces. I have checked the box that adds a public static main() method. The main is the starting point for every Java program. There must be one and only one main per project. Click Finish to create the class. With the main stub the file looks like this.



In the package explorer you now have the Program class listed under source:



A couple of notes: Notice that the "main" method starts with a lower case m. The naming convention for methods in Java is to start method names with a lower case letter and then capitalize the first letter of any following word. This is different than C# which capitalizes the first letter of method names. Also, unlike C#, the Java class name and the physical file name for that class must be the same. The Program class must be in a file called Program.java

Let's add a couple of methods to take in a name and an integer and repeat that name as many times as the integer indicates. In this example we will declare variables, get input , use an if statement to validate the input and create a loop to do the output. In short we will cover many of the basics of Java.

First we will create a new private method to get input. Next we will add two variables, a String variable called "name" and an int variable called "number." Notice that "String" starts with a capital letter. The String type in java always starts with a capital. Then we will add a Scanner object. A Scanner can read input when you place the argument "System.in" in the constructor. Eclipse will place a red line under Scanner. When you hover the mouse over the term, you will see options. One of them is to import Scanner from java.util. Do that.

Now we use the System.out.println to output prompts and the Scanner object to get the answers. There is an if statement to make sure the name is not null. At the end of the method we call the display method.



Now lets create the display method. It will use a for loop to loop through and print the name the specified number of times.



You should notice that the if and for statements are exactly the same as in C#.

Finally we have to call the Input method from main. To do this we must instantiate the Program class just as in C#. This is because, the main is static, but we did not make our other methods static.



Here is the whole program.


package com.spconger.firstProject;

import java.util.Scanner;

public class Program {

         /**
         * PROGRAM HEADER
         * This is a first Java Program
         * It takes a name and number as input
         * and outputs the name as many times
         * as the number indicates
         * Steve Conger 6/28/2014
         */

 public static void main(String[] args) {
  Program p = new Program();
  p.getInput();

 }
 
 private void getInput(){
  //variable declarations
  String name=null;
  int number=1;
  //declaring the Scanner object
  Scanner scan = new Scanner(System.in);
  //printing prompt to console
  System.out.println("Enter your name"); 
  //getting value with scanner
  name=scan.next();
  //check to make sure there is a name entered
  if (name==null){
   System.out.println("You must enter a name");
   return;
  }
  //propmpt and get integer value
  System.out.println("Enter an integer");
  number=scan.nextInt();
  //call the display method and  pass it 
                //the name and number
  displayNames(name, number);
 }
 
 private void displayNames(String n, int num){
  //loop to print out names
  for (int i=0;i<num;i++){
   System.out.println(n);
  }
 }

}


To run the project click on the green triangle on the toolbar. A dialog will appear asking if you want to build your class. Click ok. Then your prompt should appear at the bottom of your eclipse instance looking something like this:



Enter a name and a number. Your results should look something like this:


No comments:

Post a Comment