L01

Due by the end of Tuesday, 12 January 2021


SUBMIT   /   CHECK

This Week's Activities

Pre-Programming Activity
You must sign up for the protected portion of the Web site. You must do this even if you were in CSCI1226 in the fall -- it's a different web site. You will not be allowed to pass in your labs or assignments until you have signed up.
Your username for the site is your A-number. Your initial password is also your A-number. You'll choose a new password as part of the registration process.

If you were not regiatered in the course Monday morning (the 4th), then you will have to contact your lab instructor to ask to be added to the permitted list for the site.

Activity #1
Look at this C++ code:
/* Illustrates how to make a program pause and wait for the user to press Enter befoe continuing, an often useful "user interface" feature. Author: Porter Scobey Author: Mark Young */ #include <iostream> using namespace std; int main() { // create variables int someInteger; double someDouble; string someWord, anotherWord; // introduce yourself cout << "\nThis program illustrates how to create a \"pause\" in " "the output of a program,\nso that a user may read the preceding " "output before continuing. The user then \ncontinues by pressing " "the Enter key.\n"; // The following code causes the program to pause and wait // for the user to press the Enter key before continuing. // BUT ONLY IF INPUT STREAM cin IS EMPTY WHEN THE CODE EXECUTES cout << "\nPress Enter to continue ..."; cin.ignore(80, '\n'); // The following three code sections read values and tidy the // input stream before going on to read the next value. // first an integer ... cout << "\nEnter an integer value here: "; cin >> someInteger; cin.ignore(80, '\n'); cout << "The integer you entered was " << someInteger << ".\n"; // second a real number ... cout << "\nEnter a real value here: "; cin >> someDouble; cin.ignore(80, '\n'); cout << "The number you entered was " << someDouble << ".\n"; // finally a couple of words ... cout << "\nEnter two words here: "; cin >> someWord >> anotherWord; cin.ignore(80, '\n'); cout << "The words you entered were '" << someWord << "' and '" << anotherWord << "'.\n"; // Once again, we wait for the user to press the Enter key before // continuing. And AGAIN ONLY BECAUSE THE INPUT STREAM IS EMPTY cout << "\nPress Enter to continue ..."; cin.ignore(80, '\n'); cout << "\n"; }

Create a Java program class called Pausing that is a direct translation of the code above into Java. That means you make your Java program do all the things that C++ program does, and in the same order. Include comments, translated to javadoc where appropriate. Add yourself as an author.

I know that most of you have never used C++ before, but it shouldn't matter. You should know enuf about programming to be able to figure out what the code above is doing and how to do the same things in Java.

Activity #2
Look at this Pascal code:
{ A program to find the maximum of user's input. Author: Mark Young } program getmax(input, output); VAR count, number, maximum: integer; BEGIN // initialize variables count := 0; maximum := -1; // introduce yourself writeln(); writeln('Enter numbers below. Enter a negative number to end input.'); read(number); while number >= 0 do begin count := count + 1; if maximum < number then maximum := number; read(number) end; readln(); // pause writeln(); write('...press enter...'); readln(); // report results writeln(); writeln('The maximum of the ', count, ' numbers you entered was ', maximum, '.'); writeln() END.

Create a Java program class called GetMax that is a direct translation of the code above into Java. That means you make your Java program do all the things that Pascal program does, and in the same order. Include comments, translated to javadoc where appropriate. Add yourself as an author.

I know that most of you have never used Pascal before, but it shouldn't matter. You should know enuf about programming to be able to figure out what the code above is doing and how to do the same things in Java.

Activity #3
Look at this Python code:
""" Code to read a number and calculate its factorial Author: Mark Young """ # get input n = int(input('Type a number, and its factorial will be printed: ')) # check for valid input if n < 0: print('You must enter a non negative integer') else: # calculate factorial for valid input fact = 1 for i in range(2, n + 1): fact *= i print("%d! is %d"%(n, fact))

Create a Java program class called Factorial that is a direct translation of the code above into Java. That means you make your Java program do all the things that Python program does, and in the same order. Include comments, translated to javadoc where appropriate. Add yourself as an author.

I know that most of you have never used Python before, but it shouldn't matter. You should know enuf about programming to be able to figure out what the code above is doing and how to do the same things in Java.

Activity #4
Translate getmax (see Activity #2 above) to Python (see Activity #3). Include appropriate comments, including yourself as an author.

Don't worry too much about testing. Just try to use the sort of syntax you see in the Python code. I'll be generous in interpreting the code. (And it's only one point.)

Submit this/these files:

You will be graded on the following:

  1. Pausing submitted; it compiles and runs without crashing on valid input
  2. . . . all commands using cout have been translated correctly into Java
  3. . . . all commands using cin to read values have been translated correctly
  4. . . . all commands using cin.ignore have been translated correctly
  5. GetMax has been submitted; it compiles and runs without crashing on valid input
  6. . . . all read, readln and writeln commands have been translated correctly
  7. . . . the if and while controls have been translated correctly
  8. Factorial submitted; it compiles and runs without crashing on valid input
  9. . . . all input and output commands have been translated correctly
  10. . . . the if-else and for loop controls have been translated correctly
  11. All comments have been included, and translated to javadoc as appropriate
  12. GetMax.py has been submitted and is mostly correct

SUBMIT   /   CHECK