Part 1
Eclipse is an integrated development environment (IDE). It is open source and is being developed and contributed to by many individuals and organizations. It is an excellent environment in which to develop computer programs and projects.
In this first case assignment, you will learn about the environment of Eclipse. In SLP, we will develop and run a simple Java program from this environment. Before you work on Case assignment, you must go through materials in Background Information first to make sure JDK and Eclipse have been downloaded to your computer.
Case Assignment
Please follow the steps in this video tutorial to learn how to use Eclipse, and create pez project and related classes using Eclipse on your computer.
Eclipse video tutorial (Eclipse used in this video tutorial is version 3.1.1, an older version than the one you should have installed on your own machine. However, most of the features are the same in spite of some minor differences.)
Since some of the topics are beyond the scope of this class, e.g. XML, it is not required to finish all topics in this tutorial. However, it would be good for you to revisit this tutorial in other CS courses that use Eclipse. For this class, you need to cover topics from Eclipse overview till Unit testing using JUnit.
Follow the video to create projects and classes in your own PC, after you complete each topic, for example, People Example, make a screen print on the work you have finished (Note: Not the screen print on the tutorial). All screen prints need to be in one single word document.
If you are not sure how to print a screen and past it to a word document, do the following steps:
- Open a word document, minimize it to the bottom of the screen.
- move your cursor to Eclipse, activate Eclipse screen.
- On your key board, press CTRL key and PrtSc key at the same time
- Reactive the word document you open at step 1, press Ctrl and V keys at the same time on your key board.
Part 2
After case assignment material, you should be very familiar with the Eclipse environment. In this SLP assignment, you are going to write a very simple java program, and run it using Eclipse. Following is the sample code.
Please note that the name of the following java program is HelloWorld.java. The spelling of HelloWorld must be exactly the same as to the word next to "public class". Be careful about the upper and lowercase, in java, uppercase and lowercase does make a difference. And by tradition, the first letter of the class should be in upper case.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World, this is my first Java class");
}
}
SLP assignment
- Write a 1-2 page report on your experience with learning Eclipse
- Use the sample program above as a reference and write a java program with file name of greetings.java. Your program will display one or two lines to introduce yourself. If you explore the feature of System.out.print and System.out.println, feel free to use both in your program.
- After you finish the greetings.java program, run it, and make a print screen image of the result, and paste it to the end of the report you write in step 1.
SLP Assignment Expectations
The following items will be assessed in particular:
You are familiar with Eclipse environment.
Your ability to use Eclipse to write java program.
Part 3
In this case assignment, we will learn features that are common to all programming languages including Java. Next we will learn these features by examining a java application program line by line. You will have the opportunity to write a java program in SLP based on what we have studied here. Next, we will
1) Learn some basic feature of java programs
2) Introduce you to assignment and arithmetic operators in java
3) Discuss two categories of java control flow statements:
a) Selection statement (if, if else, switch)
b) Repetition statement (for loop, while loop, and do while loop)
1) BASIC JAVA PROGRAM FEATURE
- The line numbers on the left hand side of the program are not part of the program. It might or might not show up in your java editor depending on whether the line number feature is turned on. Their sole purpose is to help you debug the program. You should have learned how to set up line number feature in Eclipse at module 1.
- Next we will introduce comments. In java, compiler ignores anything after comments. Comments help us keep a good documentation of the program. The beginning of the program (before the word public)demonstrates three ways you can use comments.
- Line 1 to line 3 demonstrates /** ...*/.
- Line 5 to 7 demonstrates /* ...*/
- Both are multi line comments. Any lines of code in between (represented by ...) are ignored by compiler. The difference between these two is that the first one enables javadoc to automatically create HTML like documentation.
- Line 9 represents single line comment where only one line of text behind // are ignored by compiler.
- It is important to write down program related information at the beginning of the program: the name of the programmer, date this program is written, the purpose of this program etc.
- Notice the white space in java and the way the program is formatted? Compiler will ignore these blanks, their sole purpose is simply easier for us human to read, and compiler does not care.
- Line 11
- The word public is a class identifier. It means any classes can access this class; it is open to the public. There are also other types of class identifiers, we will discuss in later modules. There might be multiple classes in a java program; however, there can be only ONE public class.
- Introduction after class is the name of this class. Therefore, this name of this program MUST BE Introduction.java. Noticed the Capitalized letter I? In Java, it is common practice to capitalize the first letter of a class name. There should be no space between class names, and class name should not start with a number.
- All classes and methods in the programs are enclosed by a pair of {and}. Check line 12, 22, and line 15 and 21 for examples. Missing any one will give you an error message later in compilation.
- Line 13 is a single line comment for method. It is a good practice to keep record of the functions for each method.
- Line 14: the word public is a method modifier. It means the method can be accessed by methods outside this class. In addition to public method, there are also private, protected, static methods:
- privatemethods can only be accessed by members within this class. To tell the difference between a public and a private method, you can think of a watch, public methods allow you to set the time of the watch, yet private methods/members are the ones doing other intricacies inside the watch. It is good that we cannot access private methods directly, it will protect the data.
- protectedmethods/members of a class can be accessed by methods of within the same class or subclass; it can also be accessed by methods of classes that are in the same package.
- staticmethods maybe called by writing the name of the class before the method call.
- If it is not clear for you, do not worry, it will be clearer when we move on to other modules.
- I will skip the meaning for void, and String, args[] here, for the time being, just remember it. It will be clearer when we studies methods in module 3. But let's discuss main first since it is very important for java application program.
- Every java application program has a main method that is where the program starts. Compiler will first look for the main method to compile. If you have several java classes in one file, the file name will be the class name of the one who has the main method. NameofClassHasMainMethod.java.
- Line 16 declared a primitive variable, an int integer variable called studentNum; this variable is assigned value of 20. Before you create a variable, you need to declare what kind of variable it is first, followed by the name of the variable. For more information on primitive data type, click here. Therefore, to declare primitive variables, we may write
- Four kinds of Integer numbers, in ascending order of the data range they can hold
- Byte
- short
- long
- int
- Two types floating point numbers
- float
- double
- doublevariable is more precise than float variable; it can hold 15 numbers after decimal point whereas float can hold 7.
- One char variable: char grade = 'A'; //single quote, no double quote.
- One booleanvariable: boolean paid = false; // if no value is assigned to a boolean variable, by default it gets the value of false.
- notice all variable names start with lower case, if there is more than one word, the first letter of the second word is capitalized.
- One more thing needs to be emphasized: since intstudentNumis declared WITHIN the main method, it is called a local variable, meaning; only it is only recognizable with this method. Other methods in this class will not be able to use or recognize it.
- Line 16 - 20 demonstrates how to use System class to print the program output on your computer screen. System class is loaded automatically when you compile your program; it provides print and printlnmethods to print program results to your computer screen. Difference between these two is that, println method will create a line break. Try to type the above program in your Eclipse editor, and alternative println and print methods and see what happen.
- Text inside the " " are called literals. Basically, what you see is what you get for text inside the double quote.
- inline 19, you will see the sign there. It is called concatenation sign, I called it glue, and it will glue anything on the left and right, and assemble them together into a String.
- In the end, also notice most of the statements end with; It means the end of a statement. However, there should be no; at the end of a class or a method, otherwise, you will get an error message.
After you have gone through a java program line by line, revisit Java overview under Background materials in module 2.
2) ARITHMATIC CALCULATIONS IN JAVA
Click here to get to know the complete list of operators used in Java. In this module, we will introduce two kinds of operators: simple assignment operator and arithmetic operators. Latter modules will explain the other operators in great details.
Simple Assignment Operator
= Simple assignment operator
- = is pronounced as "get the value of", the value on the right hand side of the = is assigned to the left hand side.
For example: the statement
int a = 0;
int b = 3;
a = b;
now, a has the value of 3.
- = is different from = =, Whereas = = reads as "equal to", it evaluates whether the value on the left hand side is equal to the value on the right hand side, then return a boolean variable, true or false.
Arithmetic Operators
Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
- The first three operators ( , -, *) are very similar to the ones we use in algebra.
- For the / division operator
- if operands, meaning the variable/equation on either left or right hand side of /, are both the same types of variables, they get the result in the same data type. For example, if both operands are int, they will get in result, if both are float variables; you will get float variable results.
- int variable, then the result will be an int variable. For example:
- int person = 2;
- int apple = 3;
- the result of person /apple will be 1, the .5 is thrown away.
- to keep the half apple, you can add double in front of either person or apple variable. For example: (double) person/apple.
- if operands are a mixture of data types, the result will be in the same data type that has higher precision or wider data range. For example:
- int a = 6;
- double b = 2.0;
- result = a/b;
- result will be a double variable
- For the % operator, both operands need to be integer. The result will be the "left over". for example:
- int a = 5;;
- int b = 4;
- result = 5%4;
- value of result will be 1.
- % is useful when you want to calculate for example, if you have 100500 cents, how many dollars, quarters, and cents you have etc.
Check Java operators link under Required readings in Background materials for more information on how to use operators.
3) Java control flow statements
a) selection Statement (if, if else, and nested if else statement)
By default, java program will be executed sequentially, which means that code written earlier, will be run earlier. However, there are occasions where some block of codes is skipped if they cannot meet certain criteria. In 3 a), we will discuss how to use if, and if else statement.
Click on this link to study how to use these two kinds of statement. This example uses pseudo code to write java program. We will discuss more about pseudo code in module 3. For the time being, you can think of it as a way to help you think logically, and help you to write java program. In addition to
1)if
2)if else statement,
3)there is also nested if and if else statement, they just add more conditions.
For example
if (this is a sunny day)
{
Wear a hat;
}
else
{
If (it is raining)
{
Bring an umbrella
}
else if (it is snowing)
{
Wear a big coat
}
}
b) repetition statement (for, while, and do while)
In java programs, some block of codes are executed multiple times when conditions are met, statements that allow these codes have favorable treatments are:
1) for loop
2) while loop
3) do while loop
Click on this link to learn how to use them (this powerpoint also reviews and selection statement.)
Case assignment
Click here to download case assignment file. This is a PDF file, you can transfer the file into a word file and work from there.
1) After you open the PDF file, use ctrl A to select the whole PDF
2) ctrl C on your keyboard to copy the PDF file
3) After you are done, open a word file
4) put your cursor on the word file, ctrl v to paste.
Part 4
In module 2, we have briefly reviewed history of programming languages and java. We have also learned basic features of java. Many of these features are shared among other languages; the only differences are in the syntax: for example, all languages have data types, selection statement, and repetition statement. The goal for this SLP assignment is to review what we have learned by writing a simple java application.
SLP assignment
Write a java application that meets the following requirement:
- Using for loop, while loop, or do while loop to print a square which has 10 X 10 lines of # except,
- Line 2, 4, and 6 which consists of * instead of #
Therefore the final output will look like this:
##########
**********
##########
**********
##########
**********
##########
##########
##########
##########
hint: write if or if else statement within the loop.
Part 5
Case assignment in module 3 will accomplish three tasks:
- Introduce algorithm and pseudo code
- Introduce Scanner class to get user input from keyboard
- use three programs that accomplish the same task to demonstrate different styles of programming.
1) Algorithm and Pseudo code
Inexperienced programmers tend to jump in a programming project/assignment immediately. Yet experienced ones understand the importance of analyzing the problem, developing algorithm, and writing pseudo code first before start coding. Write a program without properly planning might be okay when you write a simple program to display hello world, yet very often the project is very big, needs to be tackled by a team of programmers, therefore, problem analyze and planning is crucial to the success of the project. Before we move on the next example, please check the meanings of algorithm and pseudo code.
Different from human, computer needs step by step instructions on how to perform task. For example, you make a photo copy of an important document. As human, we understand that the following steps naturally without being instructed specifically:
1) walk to copier room
2) open copier cover
3) place the document under the copier
4) press start button
Yet for computer, we need to instruct it step by step in the program in the exact order, if the step 3 and 4 change in the program, we are not going to get a photo copy. Therefore, using pseudo code to write down the algorithm of the task at hand can shorten our programming time and avoid a lot of headaches afterwards.