0% found this document useful (0 votes)
27 views2 pages

Worksheet 1.2

The document discusses code snippets for printing a countdown using a for loop, calculating the average of two numbers, and calculating a person's age given their year of birth. It also asks questions about the methods written, including whether they require inputs or have return values, and writing preconditions and postconditions. The precondition for calculating average is that two numbers must be provided, and the precondition for calculating age is that the current year and year of birth must be provided. The postcondition for calculating average is that the average is correctly calculated, and the postcondition for calculating age is that the output age correctly reflects the input values. Statements to check preconditions and print messages for violations are also discussed.

Uploaded by

Abdullahi Sharof
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Worksheet 1.2

The document discusses code snippets for printing a countdown using a for loop, calculating the average of two numbers, and calculating a person's age given their year of birth. It also asks questions about the methods written, including whether they require inputs or have return values, and writing preconditions and postconditions. The precondition for calculating average is that two numbers must be provided, and the precondition for calculating age is that the current year and year of birth must be provided. The postcondition for calculating average is that the average is correctly calculated, and the postcondition for calculating age is that the output age correctly reflects the input values. Statements to check preconditions and print messages for violations are also discussed.

Uploaded by

Abdullahi Sharof
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Worksheet 1

1 In the main() method, declare and integer variable x. Using a loop, print a countdown from x
to 0.
2 public class forLoop {
3
4 public static void main(String[] args) {
5
6 for(int i=10; i>=0; i-=2) {
7 System.out.println(i);
8 }
9 System.out.println("Happy new Years");

10 Write two methods


a) One to compute the average of two numbers.

public class AverAge {


public static int Average( int num1, int num2) {
return (num1 + num2) / 2;
}
public static void main(String[] args) {
int num1=40;
int num2=10;
int result= Average(num1,num2);
System.out.println(result);
}
}
 Will this method require any inputs?
- Yes, the method will require either a user input or a placed value. In this example
I set 2 value to be equal to num1,num2.

 Will it have a return value?


- Yes the return value will be the average of both numbers which is adding them
and dividing by 2.

b) Another method that prints the current age of a student when given the year of birth.

public class currentAge {


public static int calculateAge(int yearOfBirth, int currentYear) {
return currentYear - yearOfBirth;
}
private static int currentYear = 2024;
private static int birthYear = 2000;
public static void main(String[] args) {
int age = calculateAge(birthYear, currentYear);

System.out.println("The student is " + age + " years old.");


}
}

 Will this method require any inputs?


- Yes method will require inputs from the year of birth
 Will it have a return value?
- Yes it will. The return value will provide the age of the student.

11 Consider the methods you wrote in Q2:


a. Can you write the preconditions:
- The precondition for A. is that two numbers must be used to calculate the average.
- The precondition for B. is that the current year must be known and the year of birth of
the student.
b. Can you write the post-conditions?
- The postcondition for A. is that The average of the two numbers has been calculated
correctly.
- The postcondition for B. is the output correctly reflects the age that has been given by
the user.
Can you write some statements that detect when the precondition is violated and print a message?

- int userAge = 17;


- if (userAge < 18) {
o System.out.println("User must be atleast 18 to access this service.");
} else {
System.out.println("User age is valid..");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy