passwd
on the command line in a terminal window. You
will be asked to type your current password and then
the new password twice. Note that as you are typing your password,
nothing shows up on the screen (as a security measure).cd
followed by the path
to the folder. Use Tab
key for auto-complete.
jEdit &
This will starts
jEdit. You can starts typing your program and use "Save as" to save it
into your folder. Don't forget to name the file the same as the class
in the program and use .java extension.
javac FirstJava.java
(use your
file name instead of FirstJava). If there are
no errors,
run it: java FirstJava
(again, use your file name instead
of FirstJava). Make sure that you test for different conditions.
Write a loop that counts the number of
times a character appears in a string. For instance, 'r' appears in
the string "Morris" 2 times. In the beginning of main
create
variables for the string and the character:
String str = "Morris";
char c = 'r';
You can test your program by changing the string and the
character. Comment out your previous tests, don't delete them.
count
before the loop, add 1 to the variable every time you encounter the
character you are checking for. Print out the counter variable after
the loop. Make sure
your printing clearly states what you are printing.
Write a loop that, given a positive integer number, prints out all of its pairs of factors. For instance, when the number is 12, the program should print:
12 can be factored as:
1 and 12
2 and 6
3 and 4
If the number is a perfect square, it's ok to print its root twice:
16 can be factored as:
1 and 16
2 and 8
4 and 4
However, make sure thatt every pair is printed only once. A function
that gives you a square root of a number
is Math.sqrt
. You can use it as
Math.sqrt(16)
(which results in 4). Check what it returns
for numbers that aren't perfect squares.
Write a loop that generates random numbers in the range between 1 and
6 (inclusive) until they add up to 20 or higher. In the loop print all the numbers that you
have generated. Print the sum after the loop.
Hint: use a while
loop, not a for
loop,
since you don't know ahead of time how many times the loop needs to
run.
Send the java file(s) by email to me: elenam at morris.umn.edu. The subject of the message must be 2101 Lab 2. Make sure to CC your group partner if you worked with another person.