Download Java Exercises

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

Corecursion wikipedia , lookup

Nonblocking minimal spanning switch wikipedia , lookup

Computational complexity theory wikipedia , lookup

Delta-sigma modulation wikipedia , lookup

Pattern recognition wikipedia , lookup

Halting problem wikipedia , lookup

Planted motif search wikipedia , lookup

Transcript
CSC1030 HANDS-ON INTRODUCTION TO JAVA
Lab Exercise #2
While loop
/*
A sample program to illustrate how to use the "while" loop statement.
This program counts down from 10 to 1.
*/
package Demo1;
public class Demo_While
{
public static void main( String[] args )
{
int counter;
counter = 10;
// Start counting from 10;
while (counter > 0)
{
System.out.println(counter);
counter = counter - 1;
// Update the value of counter
}
System.out.println("Go!");
}
}
Problem 1: (10%)
Write a program to print all the odd numbers from 1 to 30 (inclusively).
Problem 2: (10%)
Write a program to repeatedly read an integer from the user until the integer is equal to zero.
1
Math exercise
package Demo3;
// You need to add this line in your program.
Watch out for letter case.
// Alternatively, we can use Fix Imports under NetBeans.
import javax.swing.JOptionPane;
// The class name is NOT Main in this example!
You may RENAME the class.
public class Demo_Math {
public static void main(String[] args) {
String
input_str;
double
a, b, c, discriminant;
input_str = JOptionPane.showInputDialog("a in ax^2 + bx + c: ");
// convert the input (text input) to coefficient a
a = Double.parseDouble( input_str );
input_str = JOptionPane.showInputDialog("b in ax^2 + bx + c: ");
// convert the input (text input) to coefficient b
b = Double.parseDouble( input_str );
input_str = JOptionPane.showInputDialog("c in ax^2 + bx + c: ");
// convert the input (text input) to coefficient c
c = Double.parseDouble( input_str );
discriminant = b*b – 4*a*c;
System.out.println( "value of b^2 – 4ac is " + discriminant);
}
}
The above program is provided for you to have a warm up exercise on your Java skills.
Problem 3(15%)
Write a program to
a) Read the coefficients a, b, and c of a quadratic equation ax2 + bx + c = 0 as three floating point
(number with a decimal point) values from the user.
b) Calculate and print a message indicating whether the corresponding equation has one, two or no
real roots.
Problem 4 (15%)
Modify your program in problem #3 so that in addition to indicating the number of real roots the
quadratic equation has, the program will also calculate and print the value of the roots (if there is
any).
2
Problem 5: (20%)
e  1
Write a program to approximate the value of e as
1 1 1
1
   .. 
1! 2! 3!
k!
where k! means k factorial = 1  2  3    (k  2)  ( k  1)  k .
Larger the k, that means more terms in the approximation, hence, more accurate the app. value of e.
Compute and print the TEN approximate values of e for k=1, 2, …, 10 respectively.
(Hint: you have to write a while-loop to calculate k factorial, for any k. Moreover, ANOTHER
loop is needed to enumerate (or count) k from 1 to 10 in order to sum the terms.)
String Exercise
Problem 6: (20%)
Write a program to read a message from the user and print the message out vertically.
For example, if the user input is “Hello”, then the output will be
H
e
l
l
o
Note: You may need to use the while statement and the String methods.
Problem 7 (challenging): (10%)
A palindrome is a string that reads the same backward and forward. For examples, “eye” and
“able was I ere I saw elba”. Write a program that reads a string from the user and check
whether the input string is a palindrome or not. After checking, print YES or NO.
(Note: Uppercase and lowercase letters are treated as different characters in the Java program.)
3