CS1027b Computer Science Fundamentals II
Lab 1
Learning Outcomes
Upon completion of this lab, you should be able to do the following in Eclipse:
- Create a new project containing new source files.
- Create a new project from existing source files.
- Import java libraries to your program.
- Create a simple java program that can read input from the keyboard and perform simple calculations.
General Lab Instructions to Help Labs Run Smoothly
- Read through the lab instructions before coming to the lab.
- Do the pre-lab preparation.
Overview and Preparation
In this course we will use Eclipse as the Integrated Development Environment. Eclipse has been installed on
the Computer Science Department machines in the Undergrad Labs used as
the First Year Teaching Environment (MC 8, 10, 230, 235).
- You can download Eclipse for your own computer from the
Eclipse Website. Read the tutorials and information posted in the course's webpage about how to install java and Eclipse on your computer.
- If you have not used Eclipse, you may want to read the Workbench
User Guide in the on-line help which comes with Eclipse (on the top menu,
select "Help" and then select "Help Contents").
IMPORTANT. Make sure you attend your lab session, sign the attendance sheet, show the result(s) of each exercise to your TA, and submit your .java files through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
Suggested Project Organization
In Integrated Development Environments such as Eclipse, Netbeans,
VisualStudio, etc. we need to create a project in order to run a program. Most
programs consist of multiple files. A
project contains all the information about the program, such as a list
of program files (source files, class files) and IDE settings.
You will create a new project in Eclipse for
every program that you wish to run.
Create a folder called (for example) Eclipse or CS1027b to hold all
your Eclipse projects for this course. This folder will contain the project
files themselves. The Java source files may be in this folder also (as
in Exercises 1 and 3), or in some other (external) folder (as in
Exercise 2).
Exercise 1: Getting Started with Eclipse
Exercise 2: Creating a New Project from Existing Source Files
In this exercise, you will create a new project to run the sample
Social Networking application whose files are posted on the
sample
code section of the CS1027 Course web page. (We will not
concern ourselves with what this code does at present; you will merely
use the files to practice this part of the Lab.)
- Create a new folder in your disk area called SocialNetworking; do not create the folder inside your workspace folder.
Download the four .java files from the "Object Oriented Programming" section of the
sample page,
and store them into that folder.
- In Eclipse, select File, New, Java Project from the top menu. Enter the project name:
SocialNetworking.
- Uncheck "Use default location", and browse to find the folder where you stored the above java files.
Click Finish at the bottom of the window.
- Go to the Package Explorer window and select the SocialNetworking project by clicking on the ">" symbol to its left.
Then select "(default package)" by clicking on the ">" symbol to its left.
- You will get a compiler error from
MyFriends.java
(the
little red "X" at the bottom left corner of the icon for MyFriends.java),
since the provided code does not yet have the getNumFriends()
method defined in SocialNetwork.java
.
Double click on the MyFriends.java icon. To fix this error, comment out the offending line of code in MyFriends.java
(add
"//" at the beginning of the line marked with the little red "X"; this indicates that everything after the "//" is a comment)
- Run the program. The results should be visible in the Console window located below the Editor window.
- The output of the program should be a list of contacts and their email addresses.
- Close the project.
Exercise 3: Creating a New Java Program
- Create a new java project called MyName in your project folder.
- Create a new class called MyName that has a
main
method which displays your name and email
address on the screen.
- Run your MyName program and make sure it works correctly.
- Close the project.
Exercise 4: Creating a Simple Calculator
- Create a new java project called SimpleCalculator in your project folder.
- Create a new class called Calculator with a
main
method.
- Add to your new class the line
   
import java.util.Scanner;
before the line
   public class Calculator {
This will tell java to include the library called java.util.Scanner
to your program.
This library contains java code that your program can use, among other things, to read user input from the
computer keyboard.
- Inside the
main
method do the following:
- Declare two integer variables
x
and y
and one
character variable c
. The syntax for declaring an integer variable is
   int variable_name;
and for a character variable
   char variable_name;
- Initialize
c
so it stores the character '+'.
- Add the following code after you have declared the variables
   
Scanner input = new Scanner(System.in);
Now, to read an integer from the computer keyboard and to store it in variable x
you can use this
code:
   x = input.nextInt();
To read a character from the computer keyboard and store it in variable c
you can use
   c = input.next().charAt(0);
- Add the following code after the code you entered in the last step:
   
while (c == '+' || c == '-') {
  }
- Within the curly brackets
{    }
of the while loop add code to perform the following steps:
- Display a message on the screen asking the user to enter a number.
- Read a number entered from the computer keyboard and store it in variable
x
.
- Display a message on the screen asking the user to enter a second number.
- Read a number entered from the computer keyboard and store it in variable
y
.
- Display a message on the screen asking the user to enter an arithmetic operator '+' or '-'.
- Read a character entered from the computer keyboard and store it in variable
c
.
- Add the following code
   
if (c == '+') System.out.println("Result = "+(x+y));
  else if (c == '-') System.out.println("Result = "+(x-y));
- Run your Calculator program and make sure it works correctly. The program must terminate when the arithmetic
operator entered is different from '+' and '-'.
- Close the project.