Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



INST0002: Programming 1

Tutorial Sheet 1

 

The aim of the practical sheet is to provide you with a set of exercises to practice developing your programming skills. Writing programmes will help you develop programming skills and prepare for the Programming Test and the Moodle Quiz.

 

Assessed Tutorial Questions:

Some questions in the labs are marked with a [*] symbol. These questions are compulsory and you will be assessed on one or more of these in the following week’s lab. For most of you, this means you will be assessed on:

· Tutorial sheet 1 in Lab 2

· Tutorial sheet 2 in Lab 3

· Tutorial sheet 3 in Lab 4

· Tutorial sheet 4 in Lab 5

· Tutorial sheet 5 in Lab 7, and

· Tutorial sheet 6 in Lab 8

 

Please look at all the questions spend some time thinking carefully about them, before asking for help. If you are still stuck:

· attend the live sessions and ask me, or the TAs, for help

· or post a question on the Moodle discussion board.

 

Once you have completed the assessed questions you should upload the specified files to the Assessment tab on Moodle under the appropriate submission link. You can submit at the specified submission time on Moodle.

 

Please refer to Read about scheduling and live sessions and Read about tutorial based assessment, submissions and vivas for additional information on the procedures of being assessed.

 

A reminder on how to compile a programme 

This is just a quick reminder of how to compile a simple Java program.

1) Make sure you organise your files into folders for each of the weeks:

a. You should keep your files on the N: drive. I suggest creating a folder called INST0002 with subfolders week01, week02 etc. for each of the tutorial files.

2) You should open the Command Prompt Shell and navigate to the current directory where your Java files are located, for example, N:/INST0002/week01. To do this, use the cd command. For example:

cd  N:/INST0002/week01

To change the drive from C: to N: simply type in N: in the command prompt shell.

Speak to a TA or a peer student if you need help with using the N drive or the Command Prompt shell.

3) Once in the working directory, you can compile the files without providing the path to the file:

javac HelloWorld.java

(where javas is the name of the name of the compiler programme you are invoking, and HelloWorld.java is the name of the file to be passed to the compiler for compiling.)

 

4) If your Java files successfully compiled, you can run these by typing in:

java HelloWorld

(where java is the name of the file that will invoke the Java Runtime Environment and load specified class)


Exercise 1*

Learning Objectives: [A] learning to edit and compile Java programmes; [B] practice working with other people’s code.

 

Referring to the following listing (marked as Fig. 2.1) we discussed during the lecture, you are asked to develop a similar programme to experience all the phases of developing a programme – from editing to execution.

 

The conditions for modifying the code should be as follows:

· The name of your public class should be Greeter.

· The programme should output the following message: Programming will help me succeed!.

· The comments should reflect the changes and your understanding of the programme.

 

 

 

Exercise 2*

Learning Objectives: learning to work with existing Java API methods, namely: print(…), pringln(…) and printf(…).

 

Write a new programme to output a pattern using the following shape three times using each of the above methods for printing:

 

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

 

You may use the following listing for your programme.

 

public class DiamondPrinter{

public static void main(String[] args){

// Option A: print(...)

// Option B: println(...)

// Option C: printf(...)

}

}

 

 

Exercise 3*

Learning Objectives: [A] using existing Java API methods, namely: printf(…); [B] Primitive types and simple arithmetic operations.

 

Extend the programme below (see PatternPrinter class) to output the following pattern using only the variables have been declared in the listing without introducing additional strings or variables.

 

Each of the line should be printed using only the printf() method and making use of the formatting capabilities. Each of the numbers following the star should be generated through arithmetic operations using the x variable.

 

NOTE: You must only use the variables declared in the code and do not introduce any other data or variables.

 

- - ->*1

- - ->*12

- - ->*124

- - ->*1245

- - ->*12456

 

public class PatternPrinter{

public static void main(String[] args){

int x = 2;

char star = '*';

String prefix = "- - ->";

//You should add the necessary code below.

}

}

 

Exercise 4*

Learning Objectives: [A] using existing Java API methods, namely: print(…), println(…) and printf(…); [B] primitive types and simple arithmetic operations.

 

You are required to solve the following simple equations and store the values of x and y in corresponding variables: 3x – 7 = 14 and 2y + 10 = 16.

 

You should then calculate what the remainder of dividing x by y should be and store the result in a variable called result. Finally, the programme should print the following:

 

The value of x is: _____

The value of y is: _____

The value of result is: _____

 

The blanks in the output should be replaced with relevant values.

 

Exercise 5*

Learning Objectives: [A] import classes and packages from Java API; [B] primitive types and simple arithmetic operations.

 

Write a programme that will take a double input from a user for the length of a radius (r), and calculate the diameter (d = 2 * r), surface area (a = 4 * π * r²) and volume (v = 4/3 * π * r³) of a sphere.  The programme should output the following:

 

The diameter of the sphere is: d = ______

The surface area of the sphere is: a = ______

The volume of the sphere is: v = _______

 

The blanks in the output should be replaced with relevant values.

 

To ensure better usability of your programme, please include a prompt message such as “Please enter the value for the radius: ”.

 

NOTE: In addition to the Scanner class from the util package (i.e. java.util.Scanner), you might consider using the Math class (i.e. java.lang.Math) for calculating the power and using the more precise value of π. Check the documentation for https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html, and https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

 

Exercise 6*

Learning Objectives: [A] import classes and packages from Java API; [B] primitive types and simple arithmetic operations.

 

Write a programme that will calculate the total number of calories burnt by taking user input of:

- Body weight (bw) in kg. (e.g. 71.5)

- Height (h) in meters (e.g. 1.73)

- Average speed (s) in meters per second (e.g. 1.4)

- Time walked (t) in minutes (e.g. 35)

All the values except time must be double, while time should be an int.

The formula for calculating the total calories burnt (tcb) is:

tcb = t * ((0.035 * bw) + (s2 / h * 0.029 * bw))

 

Use relevant user prompt messages for requesting reading user-specified data.

 

The output of the programme should be:

 

stepanyan$ java CalorieCalculator06

Please enter bodyweight (kg): 65

Please enter height (m): 1.69

Please enter average speed (m/s): 1.5

Please enter time walked (min): 20

The number of calories burnt is: 95.692308

 

NOTE: In addition to the Scanner class from the util package (i.e. java.util.Scanner), you might consider using the Math class (i.e. java.lang.Math). Check the documentation for https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html, and https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html


Exercise 7*

Learning Objectives: [a] import classes and packages from Java API; [b] primitive types and simple arithmetic operations.

 

Write a programme that will ask user to input their name. The name should be stored in an appropriately typed variable.

The programme should proceed to ask the user for three integer numbers and calculate their average.

The output of the programme should include the name the user provided as input, and print the average of the three user numbers.

The output of the programme should be:

 

stepanyan$ java AverageCalculator07

Please enter your name: Karen

Please enter Number 1: 34

Please enter Number 2: 26

Please enter Number 3: 64

Hey Karen

The average is: 41.333332

 

 

Exercise 8!

Learning Objectives: [a] import classes and packages from Java API; [b] primitive types and simple arithmetic operations, including the use of modulus (%)

 

Write a programme that will prompt the user to enter a four-digit integer, for example 8354. Using arithmetic operations identify a mechanism for separating the four-digit number to four separate digits. Store individual digits in separate variables and print them.

The sample output of the programme, given the user input of 8354, should be:

 

Enter a four-digit integer: 8354

Digits in 8354 are: 8  3  5  4