<<

Tutorial Questions Semester 1, 2020 Tutorial 1 Solutions

The first part of the tutorial should be devoted to questions you have about course material. Don’t let your tutor off lightly – they have knowledge which you can make good use of. You will be expected to engage with the tutor and discuss solutions to the problems presented here. The content covered in the tutorials will assist in your understanding of the practical requirements for the checkpoints. 1. Programming Languages What are the meanings of the terms syntax, semantics and . Make reference to these concepts in discussing: i. In what ways are programming languages (such as Java) similar to natural languages (for example, English and Thai)? ii. What particular requirements do programming languages have and how have these requirements affected their design? In what ways are programming languages different from natural languages? Solution: The syntax of a language refers to structure that sentences in the language are supposed to maintain. For example, sentences in English are composed of phrases, they start with a capitalised word and end in a full stop. In Java terms, the correct syntax for a class definition is that it must include a class-body that is enclosed in braces, simple statements are terminated with semicolons, an assignment statement has the form x = y, and so on. The semantics of a language refers to the meaning which is attached to syntactic structures. The meaning of a program is embodied in the code produced by a compiler. Typical semantic errors are "type" errors. For example: int x = "2345"; This conforms to the correct syntax for the Java language but when the meaning of this declaration of the variable x and the initialisation assignment is examined there appears to be an incompatibility. x is declared of type int but we are attempting to assign a value of type String to x! 2. Identifiers Which of the following are valid Java identifiers and which of those are appropriate (are they meaningful)? i. g_1 – valid but not meaningful ii. max_speed – valid and meaningful iii. class – not valid as it is a Java reserved word iv. 4sale – not valid as it starts with a digit v. _1_2_3 – valid but not meaningful vi. x – valid, meaningful as a coordinate 3. Basic Structure of a Java Application (Program) Identify the syntactic elements which make up the following Java program, including i. the major elements (classes, methods and statements) and their components, and ii. reserved words and identifiers, and iii. string, integer and floating point literals.

COMP1102/8702 Computer Programming 1, CHM & LJS 1 College of Science & Engineering, Flinders University Tutorial Questions Semester 1, 2020

class Sum { public static void main(String[] args) { System.out.print("The sum of the numbers " + 4 + " and " + (5 + 10)); System.out.println("is " + (4 + (5 + 10))); } // end of main } // end of Sum

Solution:

• The class definitions are the highest level structures in a Java program. • Method definitions like main are contained inside class definitions (and nowhere else!) • Methods can contain local variable declarations and program statements. You will have used the System class to write program statements that print information to the screen of a running Java application. System is a class defined in java.lang (imported automatically) and out is a variable which is bound to an object representing the "standard" output stream The important point one should consider when looking at statements such as System is that the "." notation is used to indicate that something is contained in something else (which may be a package, class, variable, etc. . . ) • Braces are used to begin and end classes and methods. • Identifiers are the words of the program: class, Sum, public, static, void, main, String, args, System, etc. . . . • Reserved words are identifiers that have special meaning and cannot be used for variable, method, or class names. For example, class, public, and void. • Literals are explicit values. There are 3 string literals and 6 integer literals in the program. The string literals are "The sum of the numbers", " and ", and "is ". The integer literals are 4, 5, and 10, which all appear twice. There are no floating point literals. iv. What do "System" and "out" refer to? Solution: java.lang.System.out java is a package which contains the package lang which contains the class System which contains the variable out v. What will the program output? Solution: The program will output: The sum of the numbers 4 and 15 is 19 . . . note that it is all on one line.

COMP1102/8702 Computer Programming 1, CHM & LJS 2 College of Science & Engineering, Flinders University Tutorial Questions Semester 1, 2020

4. Compilation and Syntax Errors i. Explain what a compiler does and what part it plays in the development of programs.

Solution: Compilers perform syntax and some semantics checking and translate the source program (the .java files) into bytecode (.class files) for the Java Virtual Machine. It should be viewed as a tool. ii. What is the difference between a syntax error and a logical error? Provide examples of syntax and logical errors. What is the other type of error that is encountered in programming? Suggest an example. Solution: Syntax errors are errors detected by the compiler that do not conform to the syntactic rules of the Java language. The correct use of words and symbols of the Java language will avoid syntax errors. A logical error is an error that produces an unexpected result in the output. This type of error is not detected by the compiler. An example of a logical error could include the multiplication of two numbers as opposed to the intended addition. Another type of error is a run-time error. This is an error that is not detected by the compiler and may not present itself in every execution of the application. A run-time error will cause the application to crash during execution. An example of a run-time error would be a division by zero operation. iii. Identify the syntax errors in the following program. Explain why each error has occurred and how it could be corrected. 1 class Errors { 2 public static vod main(String[] args) // { 3 system.out.println(5 + " is larger than " + 6) 4 system.out.println("x = + 4 + ", y = 5" ; 5 } 6 }

Solution: vod ==> void remove // on line 2 system should be System (twice) missing semicolon on line 3 missing ) and " in line 4 iv. Categorise each of the following situations as a compile-time error, run-time error, or logical error. a) multiplying two numbers when you meant to add them – logical error b) dividing by zero – run-time error ) forgetting a semicolon at the end of a programming statement – compile-time error d) spelling a word incorrectly in the output – logical error e) producing inaccurate results – logical error f) typing a { when you should have typed a ( – compile-time error

COMP1102/8702 Computer Programming 1, CHM & LJS 3 College of Science & Engineering, Flinders University