CSE 201 - Lab 2 - Primitive Types, Strings, and Keyboard I/O


Note: This lab assumes that you have completed Lab 1, and that you know how to login to your account, how to use Eclipse to create a project and open, edit, save your files, as well as compile and run your programs, and how to submit your work with the Submit tool available from the Windows desktop on the CSE PCs. If you have forgotten any of these techniques, refer back to the Lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs are not formatted properly or do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a small Java program to practice the use of primitive types, the String class type, and simple I/O (using Scanner).


Overview

You will be writing a Java program from scratch. The program will ask the user for some inputs, it will perform some specific computations, and it will output the results.


Materials Provided

For this lab no other files are necessary.


Set up

Start Eclipse, create a new project, Lab2, and create a new Lab2 class (the corresponding file name will be Lab2.java) in the project. Complete the Lab2 program according to the description below. If you don't remember how to create a project or a class in Eclipse, see Lab 1 for instructions.


Description

Here is a sample interaction between a user and the program you will write in the Lab2 class (user inputs are in bold):
    ***************************************
    * Welcome to your first Java program! *
    ***************************************

    *** Test integer arithmetic ***

    Enter first integer number: 201
    Enter second integer number: 123
    201 + 123 = 324
    201 - 123 = 78
    201 * 123 = 24723
    201 / 123 = 1
    201 % 123 = 78

    *** Test real arithmetic ***

    Enter first real number: 3.14159
    Enter second real number: 2.718
    3.14159 + 2.718 = 5.85959
    3.14159 - 2.718 = 0.4235899999999999
    3.14159 * 2.718 = 8.53884162
    3.14159 / 2.718 = 1.1558462104488594

    *** Test String operations ***

    Enter a string of characters: The Lord of the Rings
    The length of string "The Lord of the Rings" is 21
    Enter an integer between 0 and 20: 10
    The character at index 10 of string "The Lord of the Rings" is 'f'
    Enter another string of characters: Lord
    The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4

    *** One last test ***

    Enter your birthday (mm/dd/yyyy): 10/25/1983
    You were born on 25-10-1983

There are five main parts to this program:

  1. Output a welcome banner;
  2. Input two integer numbers, compute the sum, subtraction, multiplication, division, and modulus of the two numbers, and output the results;
  3. Input two real numbers, compute the sum, subtraction, multiplication,and division of the two numbers, and output the results;
  4. Input a text string, output its length; input an index within the string, and output the character at that position in the string; input another text string, and output the location of the first occurrence of the second string in the first one (if such location exists, otherwise output -1);
  5. Finally, input a date (a text string) in the format mm/dd/yyyy (i.e., two digits for the month, followed by a '/', followed by two digits for the day, followed by a '/', followed by four digits for the year), and output it in the common European format dd-mm-yyyy (i.e., two digits for the day, followed by a '-', followed by two digits for the month, followed by a '-', followed by four digits for the year.

Note that the Java Scanner component used to get the inputs has a somewhat unexpected behavior when you have to input text after a number. This behavior is documented and explained in the textbook in the section "Mixing Calls to nextLine with Calls to Other Scanner Methods", pp. 86-89.

Important note: Part of the objective of this assignment is for your program to produce exactly the same output as the sample provided above. If you understand how the Java output statements work, you should have no problem reproducing the above interaction. This is a requirement to earn full credit for this lab assignment.


Lab Submission

Make sure your program compiles and runs correctly before submitting. To submit, use the Submit tool available on the Windows desktop to submit the Lab2.java file from the Lab2 project (the location of the project in the file system will be z:\eclipse\workspace\Lab2). Make sure that you select lab2 from the list of Assignments. If you don't remember how to use the Submit tool, see Lab 1 for instructions.