Week1Exercises

advertisement
CSCI2014 Software Construction with Java
Review Exercises
1 Review Exercises
Objectives
The objectives for the first week of CSCI2014 is to

get back into the swing of writing Java programs;

use primitive types, arrays and control structures,

use Java SDK class String methods

write simple classes;

write and use methods;
Bookmarks
Locate and bookmark the following web sites:

The module website, http://www.cse.dmu.ac.uk/~lz/CSCI2014

The website for the recommended text book, http://www.wiley.com/college/horstmann

The Java classes and API's www.cse.dmu.ac.uk/Local/System/doc/java

The home of Java: java.sun.com

The definitive Java Tutorial site: java.sun.com/docs/books/tutorial
Reading
The book that should be purchased to accompany the module is:
Author: Cay Horstmann
Title: Big Java
Publisher: Wiley, 2002
ISBN: 0-471-40248-6
The following sections of Horstmann provide background reading and reference material for this set of Exercises:

Chapter 1 to review the object-oriented software development process.

Chapter 2, an introduction to objects and classes

Chapter 3, primitive data types and Strings.
Your notes and exercise from year 1 Java modules should also provide you with most of the necessary information.
ek/lz/csci2014/Week1Exercises.doc/sep03
Page 1 of 4
CSCI2014 Software Construction with Java
Review Exercises
Practical Exercises
1
Create a separate directory called CSCI2014 for your work on this module. Also create a sub-directory called
Exercise1 for the programs in this set of exercises. In future weeks create separate directories and sub-directories
so that you have an organised set of files which you can locate with ease.
2
“Warm up” with Java – if it is some time since you last wrote a Java program!
Use a text editor to write the following code. Save it as a file Hello.java
class Hello {
public static void main (String[] arg) {
System.out.println("Welcome to CSCI2014");
}
}
Identify the main parts of the program. Pay attention to the syntax (braces, semicolons etc.) and indentation.
Compile and execute the program. Recall that the compiler and Java interpreter are invoked by the commands:
3
$
javac Hello.java
// to compile the program Hello.java
$
java Hello
// to invoke the main method in Hello.class (i.e. executes the program)
Run the following program to print out the even numbers from 0 to 20.
class LoopTest {
public static void main (String[] arg) {
for ( int i=0; i<=20; i=i+2 ) {
System.out.println(i);
}
}
}
a)
Modify the program to
(i)
output the even numbers up to 30;
(ii)
output the even numbers in reverse order;
(iii)
output the odd numbers between 0 and 20;
b) Rewrite the programs above using a while loop, in place of a for loop.
ek/lz/csci2014/Week1Exercises.doc/sep03
Page 2 of 4
CSCI2014 Software Construction with Java
Review Exercises
4
a)
Add more lines of code to the main method below in order to test the getGrade() method.
class GradeTest {
public static String getGrade(int x) {
String grade = "Fail";
if (x >= 40 )
grade = "Pass";
return grade;
}
public static void main (String[] arg) {
System.out.println("30 is a " + getGrade(30) );
}
}
b) Extend the getGrade() method to return "Credit" for marks from 60 to 74, and "Distinction" for marks of 75
and above. Test the program thoroughly, including all boundary values.
5
Locate the API for the java.lang.String class. The following exercise is to practice using some of the methods in
the String class.
a)
Run the following program. Then extend the program by replacing the comments with the appropriate code.
class StringTest {
public static void main (String[] arg) {
String forename = "Fred";
String surname = "Bloggs";
String fullname = forename + " " + surname ;
System.out.println( forename.length() );
System.out.println( fullname );
//output fullname all in uppercase
//output length of fullname
//char ch1 = initial of forename
//char ch2 = initial of surname
//output intials in lowercase
//declare a variable to hold an email address
//create an email address of the form: initials@dmu.ac.uk
//output the email address
//output the part of the email address after the @ symbol
}
}
b) Extend the program further to count the number of vowels in fullname. You will need to write a loop to
check each letter of the string in turn.
ek/lz/csci2014/Week1Exercises.doc/sep03
Page 3 of 4
CSCI2014 Software Construction with Java
Review Exercises
Writing and testing simple classes
6
An object is a structure that contains a group of attributes and a set of methods that may be used to access or
modify the attributes. Standard methods are the get and set methods, and the toString() method.
/* An appointment is for a person at a given time.
The time is represented by a 24-hour clock number, 0000..2400 */
class Appointment {
private String name; //name of person to meet
private int time;
//appointment time
public Appointment(String name, int time) {
this.name=name;
this.time=time;
}
public void setTime(int aTime) { time = aTime; }
public String getName() { return name; }
public int getTime()
{ return time; }
public String toString() {
return "Appointment with " + name + " at " + time;
}
}
a)
Write a test program to create instances of the Appointment class, and test the methods e.g.
Appointment apt = new Appointment ("Fred", 1230);
System.out.println( apt );
(Note there is a subtle error in the design of this class... see if your testing can discover it.)
b) Create an array of ten Appointments, and assign an appointment into each cell. Then write a loop to output
all the appointments in the array. You may have to review your knowledge about arrays.
Appointment[] diary = new Appointment[10];
diary[0] = new Appointment(...);
etc.
c)
(Harder) Write a fragment of code to find the earliest appointment in your array.
a)
Design and test a simple classes for a football result with attributes hometeam, awayteam, homescore,
awayscore.
7
b) Include a method named getResult() which returns a string describing the result, e.g. "Home win", "Away
win", or "Draw".
public String getResult() { ... }
c)
(Extension) Create an array of results for a particular team. Then write some code (or a method) to output
the team's record as seen in one row of a league table:
Played,
Won ,
ek/lz/csci2014/Week1Exercises.doc/sep03
Drawn,
Lost,
Goals For,
Goals Against,
Points.
Page 4 of 4
Download