Download Exercise 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ECCE
Exercises 1:
Software development
Predict the outcomes of the following programs and write them in your logbook. You will
need to use your textbook for anything which isn't obvious or which you can't make a logical
guess about. Then type the programs in, compile and run them.
Unlike the very first Hello program, you run most of these programs by typing parameters (in
bold in the exercises) on the command line after the filename of the Java class. Programs can
then read these parameters and use them as input. Input is quite hard in Java, so in the
beginning, getting input via the command line, as in these programs, is the simplest way.
Write down the results and why your predictions were right or wrong in your logbook. Note
that some programs might have syntax errors that you need to correct first!
__________________________________________________________
Hello.java
// Text-printing program
class Hello {
public static void main (String[] args) {
System.out.println("Hello!");
} // end method main
} // end class Hello
__________________________________________________________
Hello1.java
class Hello1 {
public static void main(String[] args) {
System.out.println("Hello "+ args[0] + "!" );
} // of main
} // of class Hello1
Compile and then run by typing:
C\jwork\myprogs> java Hello1 Jerry Maguire
(assuming your programs are in C:\jwork\myprogs)
__________________________________________________________
Hello2.java
class Hello2 {
public static void main(String[] args) {
String student = "I am a student";
System.out.println("Hello, "+ Student + "!" );
} // of main
} // of Hello2
No parameters this time, just type
C\jwork\myprogs> java Hello2
___________________________________________________________
Salary.java
class Salary {
public static void main(String args[]) {
int salary = 50;
salary = salary * 5;
System.out.println("The weekly salary is £" + salary);
} // of Salary
C\jwork\myprogs> java Salary 300
Updated 26/09/05
1
ECCE
___________________________________________________________
Root.java
class Root {
public static void main(String args[]) {
int number = 0;
number = Integer.parseInt( args[0] );
System.out.println("The square root of "
+ number
+ " is "
+ Math.sqrt(number) );
} // of main
} // of Root
C\jwork\myprogs> java Root 263
___________________________________________________________
Programming assignments (for lab and postlab activity)
Now some new exercises for you to try, but first, study the 'arguments.java' program - this will
be a great help.
i)
Write a Java program (Repeat.java) to print out all the words you typed in the command
line, e.g.
C\jwork\myprogs> java Repeat this is a test
should produce the output below:
'this is a test'
ii)
Write a Java currency converter program (e.g. from pounds to Euros), where the amount
in pounds is entered on the commend line.
iii) Write a Java program to find the average of all the numbers entered on the command
line.
iv)
Modify the Salary program above so that it really does take the daily salary you enter on
the command line and use it to print out a weekly salary.
v)
Run the Hello1 program without entering any parameters on the command line. Just
record what happens and see if you can make sense of the Java messages you get.
vi)
Modify the Hello program by using the class Scanner.
All these exercises should be carefully written up in your logbook. Listings should be written
into or stuck into to your logbook, never kept loose. You should also write down exactly what
happened as you were compiling and running the programs. One of the roles of a logbook is
to keep a factual record of tests done - without this, developing quality software is impossible.
Updated 26/09/05
2