Download PRACTICAL 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
TECHNICAL PROGRAMMING II (Java)
PRACTICAL 4
PRACTICAL REQUIREMENTS:
 Make use of comments




specify your name and student number
brief description of what the program does
when necessary include comments throughout your program
Use meaningful class, method & variable names.
Align statements.
Use blank lines for readability.
Test your programs thoroughly
QUESTION 1
Write a Java program that will simulate the tossing of a coin for a number of tosses. Use JOptionPane for
all input and output requirements. The user must be prompted to enter the number of times the coin must
be tossed. Your program must determine and display how many times heads and tails occurred. Take
note that Heads = 0 and Tails = 1 (generate random numbers 0 and 1).
QUESTION 2
Write a complete program that does the following:
 Asks the user to enter a word.
 printBackwards() METHOD

Prints out the word backwards.
ispalindrome() METHOD
Determine whether the word is a palindrome or not, and print out a suitable message. A palindrome is
word that is spelled the same forwards and backwards. Please note that the word should not be case
sensitive.
For example:
Enter a word:
Abba
Your word spelled backwards is:
abbA
This word is a palindrome.
1
QUESTION 3
Write a complete Java program that will do the following:
The user will enter a sentence with total length less than or equal to 80 characters long, including the
spaces.
Your program must:
1) Check that the length is greater than zero and less than or equal to 80. Print an error message
otherwise.
2) Count the number of words. (There can be more than one space between words).
3) Check that there is more than one word. Print an error message otherwise.
4) Determine how many characters the sentence still needs to have a total length of 80 characters.
Distribute that number of spaces evenly between the words so that the sentence spans the
complete 80 characters.
Example
Assume the user enters the following sentence:
You need to test your program on different kinds of data
The length is 56 characters; the number of words is 11. That means that 80 – 56 = 24 spaces need to be
distributed evenly between the 11 words (10 gaps between the words).
24/10 = 2 remainder 4
This means that 2 spaces need to be added to all the gaps between the words. The remaining 4 spaces
need to be added to the first 4 gaps (one per gap).
Result:
You
need
to
test
your program on different kinds of data
NOTE: Your program must not be written for this specific example, but for any possible sentence that the
user might enter.
QUESTION 4
The Fibonacci numbers are a famous sequence of numbers. The first and second positive Fibonacci
numbers are both 1. Thereafter each Fibonacci number is the sum of the previous two Fibonacci numbers.
Here are the first 12 positive Fibonacci numbers.
1 1 2 3 5 8 13 21 34 55 89 144
You are required to write a program that will display these 12 numbers to the user.
2