Question: Lab 2 Flow Charts and Pseudo Code Conditionals Pts 1 2 / 3 extra Mini - lecture: We use pseudo code and flow charts to

Lab 2 Flow Charts and Pseudo Code Conditionals
Pts 12/3 extra
Mini-lecture:
We use pseudo code and flow charts to capture the logic of a program prior to implementing it with code. In general practice, flow charts are too cumbersome for large programs and so they are mainly used for isolated sub-routines, etc. We use pseudo code all the time. The actual pseudo code can become documentation for the written program by converting the pseudo code statements to programming comments.
Note that we are refining our pseudo code here by adding the class and main sections and declaring the variables that we will use in our program and specifying their type (num, boolean, or String).
This will transition us nicely into programming with java.
Class is the outer most container element for the pseudo code. You will see as we get into java that a class is implemented as a file. The name of the class will match the name of the java file: MyClass will be in MyClass.java.
There are two types of classes. The main class is the class that contains the special method (called main) that is run when the program is executed. Every java program must have a main class. A model or object class defines objects that we can use in our programs. You cant run a model class. (We will do more with model classes later in the next course. For now, when we start java we will only have main classes.)
Note the pseudo code syntax. The entire program is in a container class ... end class. In a main class, the main method is main()... return and all the code statements for the program are located there. The main()... return block corresponds to and replaces the start and end statements that we used previously and the entire block is now in a class. Note how we indent our blocks.
class ClassName
main()// this was start
//code goes here for our programs
return // this was end
end class
Also, lets begin to further refine our pseudo code to be specific about communication with our program users. So, every time we get input we will prompt the user like this. (Ive actually had you do that from the beginning!) :
num birthMonth // declare a variable called birthMonth and specify that it holds a number
output Enter your birth month [112]// prompt the user for the input
input birthMonth // get the input and store it in birthMonth
The pseudo code on the slides occasionally to save space will do multiple inputs on one line like this:
input birthDay, birthMonth, birthYear
DONT DO THIS!
Do one input per line and have a separate prompt for each. This is how you have to get the inputs in the Java code so it will help to do it this way in the pseudocode.
Here is an example of the code we did previously on the board in class for doubling a number using our new pseudo code conventions:
class DoubleMyFun
main()
// Declare variables (this is a comment)
num valueToDouble
num doubledValue
// Prompt and input
output Enter the number you want to double
input valueToDouble
// Process
doubledValue = valueToDouble *2
// Output
output The value you entered + valueToDouble + doubled is + doubledValue
return
end class
Variables:
We add some more details to our pseudocode and include variable declarations.
num yearOfBirth // a number variable
String ynChoice // a String variable to hold the user response to Yes or No?
boolean isRaining // a boolean which is either true or false
We put our declarations at the top of the block immediately after the main()(see in the example pseudocode immediately above!)
Note that you only declare the type of the variable one time when you declare it. After that you only refer to the variable by its name:
num myAge =61
print myAge
Mentioning the type again is an error because you are re-declaring a variable that already exists.
Conditional Structures:
So far, our programs have been pretty simple, now we are going to add conditional processing to our skill set.
Simple If:
if BOOLEAN EXPRESSION then
statement(s)
end if
If Then Else:
if BOOLEAN EXPRESSION then
statement(s)
else
alternate statement(s)
end if
Cascaded If:
if BOOLEAN EXPRESSION then
statement(s)
else if BOOLEAN EXPRESSION then
alternate statement(s)
else if BOOLEAN EXPRESSION then
alternate statement(s)
else if BOOLEAN EXPRESSION then
alternate statement(s)
.....
else // see how there is no test here? This is every other case.
alternate statement(s)
end if // or endIf
Lab:
1. Just submit this document (See directions below.) and insert your work here.
2. For each of the following tasks, provide the complete pseudo code. Include the new pseudo code elements: class ... end class, main ... return, user prompts, and declare all variables that the program needs. You dont ha

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!