0% found this document useful (0 votes)
36 views

MSC / Pgdip It Programming: Exercise 1

This document outlines 3 exercises for a programming lab. Exercise 1 has students write a program to get a user's first and last name and output their full name. Exercise 2 has students read values from a file, including a radius, and output the calculated area to multiple consoles and a file. Exercise 3 similarly extracts a radius from a file and outputs the area.

Uploaded by

Muhammad Ikhwani
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

MSC / Pgdip It Programming: Exercise 1

This document outlines 3 exercises for a programming lab. Exercise 1 has students write a program to get a user's first and last name and output their full name. Exercise 2 has students read values from a file, including a radius, and output the calculated area to multiple consoles and a file. Exercise 3 similarly extracts a radius from a file and outputs the area.

Uploaded by

Muhammad Ikhwani
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

MSc / PGDip IT PROGRAMMING

Lab Exercises: Week 1, Day 2


First Programs

This is Lab 2, which has 3 exercises.

Exercise 1

Write a program to get the users first and last name and output their full name.

Your program should create a Console object and write a message Please enter
your first name. The program should then read a word from the console, storing it in
a variable called firstName. You should repeat this process to get the last name from the
user. You should then output the message Hello <full name>. Welcome to
Glasgow.

Your program should also print out each variable you create as soon as they are given values.
This should be printed using System.err and is an example of using trace statements.

Exercise 2

Prepare a file that contains the single line.

radius = 7 metres

The spaces are important. Also it is vital that you include a carriage return (end of line) after
the word metres!

You can chose your own filename and put it in any convenient place in your filesystem.

You may use any suitable tool (except a Java program!) to prepare this file.

Now write a program to read the contents of the file as 4 separate items, a string, a string, a
double and a string. Note that FileIn has a readDouble method as well as a
readWord method.

Write a message of the form The area is <correct area> on the following
outputs: a Console called con1, a second Console called con2 (just to show you can
have more than one console) and a file called Lab2out. Dont worry if you have a lot of
decimal places.

Exercise 3

This exercise is similar to exercise 2.

Read a complete line from your input file, storing the result as a String in your program.

Now extract the radius information from this String and write the area message on a single
Console called con.

MSc/PGDip IT Introductory Programming Week 1 Lab 2 1

You might also like