School of Informatics, University of Edinburgh

School of Informatics, University of Edinburgh Computer Science 1 Ah CS1Ah Solution Sheet 1 Simple Java Programs This is a solution set for CS1Ah Qu...
Author: Teresa Stokes
27 downloads 2 Views 89KB Size
School of Informatics, University of Edinburgh

Computer Science 1 Ah

CS1Ah Solution Sheet 1 Simple Java Programs This is a solution set for CS1Ah Question Sheet 1. You should only consult these solutions after attempting the exercises. Notice that the solutions are samples — alternative answers may be possible. Java programs for the solutions are available on-line at: http://www.inf.ed.ac.uk/teaching/classes/cs1/CS1/Ah/Questions/Java/SimpleJavaProgs

1. Experiment with the programs given in Lecture Note 3. First, type each one in, compile it, and run it. (If you have any difficulty, first work through Practical 2 from the Introduction to the System lectures, and consult a laboratory demonstrator). a) For the SumTwo program in Lecture Note 3, • What happens when you call SumTwo with too many arguments? The extra arguments are simply ignored: java SumTwo 3 4 5 prints the answer 7.

• What happens when you call it with too few? If you execute the command java SumTwo 3 the system responds with a long error message: the most important part of the message is the bit that says Array index out-of-bounds exception. This kind of error is called a run-time error since it happens when you run the program. The error is caused by the expression args[1]. The variable args is a data structure called an array which has successive elements args[0], args[1], . . . , for each argument that is given on the command line (notice how arrays begin counting from 0). If there is only one argument, only args[0] is defined, and attempting to access other components of the array will give an error message. We will study arrays in a lecture soon.

• What happens when you execute the command java SumTwo 7 david ? You get another error message. It looks like this: Exception in thread "main" java.lang.NumberFormatException: david at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at SumTwo.main(SumTwo.java:6) Errors in Java are called exceptions. The bold part of the message is the exception name. The name suggests that there was a mistake in the format of a number, and shows you the number “david” which was problematical. The remaining parts of the error message tell you where the error was encountered in the program, and is called a stack trace. It tells you which methods were active when the error was encountered.

1

School of Informatics, University of Edinburgh

Computer Science 1 Ah

b) Modify the DiffSquares program to make a SumSquares program. This should output the sum of the squares of two command line arguments. Change the name of the class, the diffSquares method, and remember to store the new program with the right filename. 



// SumSquares.java:



calculate the sum of two squares

class SumSquares { static int sumSquares(int a, int b) { int asquare; int bsquare; asquare = a * a; bsquare = b * b; return asquare + bsquare; } public static void main(String[] args) { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); System.out.print("The sum of squares is: "); System.out.println(sumSquares(x,y)); } }



c) Write a program CalculatePrice.java. This program should have a class called CalculatePrice with a method totalPrice that has parameters baseprice and packingprice. It should return a total price based on adding the two and then calculating VAT (at 17.5%) on the total. Since we use integers, all prices should be given in pence. Write a main function which invokes your totalPrice method.



// CalculatePrice.java: calculate VAT on base price & packing class CalculatePrice { static int totalPrice(int baseprice, int packingprice) { int vat = (baseprice + packingprice) * 175 / 1000; return baseprice + packingprice + vat; } public static void main(String [] args) { int base = Integer.parseInt(args[0]); int packing = Integer.parseInt(args[1]); System.out.println(totalPrice(base,packing)); }

}



d) Find the documentation of the System class in the Java 1.4 API documentation, beginning from the CS1 web pages. The amount of detail in the API documentation 2

School of Informatics, University of Edinburgh

Computer Science 1 Ah

is a bit daunting at first, but will be very useful to you later. Once you have found the documentation for the System class, find the field out. This is a static object which belongs to the PrintStream class. Now find the documentation of PrintStream. How many different print and println methods are there? There are 9 print methods and 10 println methods (why one more?). Each method has approximately the same kind of behaviour, but must be duplicated because the types of its arguments are different, which means that the precise behaviour is different. The way that an integer is displayed is different to the way that a floating point number is displayed, for example. When you use print or println, the compiler automatically selects the correct method according to the type of the arguments you write. This use of the same name for different methods is called method overloading.

2. Lecture Note 6 introduces ways of combining Java statements to achieve different control paths in a program. The “flow of control” is the path taken through the program when it runs. Suppose S1 , S2 , S3 and S4 are Java statements. Test your understanding of Java’s control structures by completing the table below to show the possible paths through the statements given. Some lines have already been filled. Statement

Number of paths

Possible paths

S1 ; S2 ; S3

1

S1 −→ S2 −→ S3

S1 ; { S2 ; S3 }

1

S1 −→ S2 −→ S3

if (x==0) S1 else S2

2

S1 , S2

S1 ; if (x==0) S2 else S3

2

S1 −→ S2 , S1 −→ S3

switch (x) { case 0: S1 case 1: S2 ; break; default: S3 }

3

S1 −→ S2 , S2 , S3

S1 ; while (x

Suggest Documents