0% found this document useful (0 votes)
136 views

Tutorial 3

The document provides instructions for 3 programming activities - asking a user for their age and printing a message depending on their age range, writing a program to check if a year is a leap year, and writing a program to solve a quadratic equation by taking input from the user and determining the number and values of the roots. It includes the expected output, detailed instructions on the logic and formulas to use for each activity, and directions to submit the Java programs as a zip file.

Uploaded by

Dung Nguyễn
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)
136 views

Tutorial 3

The document provides instructions for 3 programming activities - asking a user for their age and printing a message depending on their age range, writing a program to check if a year is a leap year, and writing a program to solve a quadratic equation by taking input from the user and determining the number and values of the roots. It includes the expected output, detailed instructions on the logic and formulas to use for each activity, and directions to submit the Java programs as a zip file.

Uploaded by

Dung Nguyễn
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/ 3

Programming 1

Tutorial 3
Activity 1

Task

Ask the user for his age. If his age is less than 13, print the message “Not for kids”, or if it's
greater than 19, print “You’re too old”. If his age is within the range [13, 19], show message
“Welcome, teenager”.

Expected result:

What's your age? 15


Welcome, teenager!

What's your age? 11


Not for kids!

What's your age? 20


You're too old!

Activity 2
Leap years are years with 366 days. Write a program to check if a year is a leap year. Test your
program with different years to make sure it works correctly.

Expected result:

Enter the year: 2018


The year 2018 is not a leap year.
Enter the year: 2012
The year 2012 is a leap year.

Instructions:

Following is the method to identify a leap year:


1. If it is not divisible by 4 (e.g. y % 4 != 0), it's not a leap year, show a message and stop.
Otherwise, move on.
2. If a year is divisible by 4, but not 100, like 2012, it is a leap year, show a message and stop
here. If a year is divisible by both 4 and 100, like 2000, continue.
3. If a year is divisible by 100, but not 400, like 1900, then it is not a leap year. If a year is
divisible by both, then it is a leap year.

Activity 3

Task

Write a program to take 3 real numbers a, b and c from user and solve the quadratic equation:
ax2 + bx + c = 0
Show a message to tell the user if the equation has one, two, infinitely many roots or none, and
show the value(s) of x.

Expected result:

Please enter a: 2
Please enter b: 2
Please enter c: -4
The equation has two roots:
x1 = 1.0, x2 = -2.0

Instructions:

These are the rules for solving the quadratic equation:


- If a, b and c are all zeros, there's nothing to be solved.
- If a and b are zeros and c is not, the equation has no root.
- If a is zero while b and c are not, there is one root: x=−c /b
If none of the above cases are met, we can apply the quadratic formula.
- If b 2 – 4 ac <0 , the equation has no real root.
- Otherwise, there are two roots:

−b+ √ b2 −4 ac
x 1=
2a
and

−b−√ b2−4 ac
x 2=
2a
Use Math.sqrt() to calculate the square root of a number (or expression). For instance:
double x = Math.sqrt(10); // calculate square root of 10
double dsqrt = Math.sqrt(b * b – 4 * a * c);

Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.

You might also like