AP Computer Science Lesson Introductory Java Language Features (5)
Hey Steemers! Here's another Java lesson! Learn much!!!
Bell Work (No Quiz Today Steemers, but answer this and you'll get an upvote!):
Analyze what the following code prints:
public class WhatDoIDo {
public static void main(String[] args) {
int i;
int n = 5;
int number1 = 1;
int number2 = 1;
int number 3 = 1;
System.out.print(number1);
System.out.print(number2);
for (i = 1; i = n; i++) {
number3 = number1 + number2;
System.out.print(number3);
number1 = number2;
number2 = number3;
}
}
}
Classwork:
Use the mod (or remainder) operation, %, to ask the user what power of i they want.
Have the program ask the user for the exponent, say i^3451. Your program will find the remainder of the exponent (with respect to 4) and will use the remainder to print strings with the following answers “i”, “-1”, “-i”, “1”. Do this with an if statement.
- Remember that // is a comment, so the computer does not read it. I will use these to describe what the code is doing. For your learning!
import java.util.Scanner;
public class PowerOfI {
public static void main(String[] args) {
// We need to first ask the user what power of i they want.
System.out.println("What power of i would you like? Please type an integer.");
Scanner input = new Scanner(System.in);
int value = input.nextInt();// Now, we do the math. % means remainder, so 25%4 would be 1 for example.
int newValue = value % 4;// Finally, the if statement.
if (newValue == 1) {
System.out.println("The value of i to the power of " + value + " is i.") }
if (newValue == 2) {
System.out.println("The value of i to the power of " + value + " is -1.") }
if (newValue == 3) {
System.out.println("The value of i to the power of " + value + " is -i.") }
if (newValue == 0) {
System.out.println("The value of i to the power of " + value + " is 1.") }
}
}
Example of the display
If the user said "45", so they wanted i^45, the program would show:
What power of i would you like? Please type an integer.
45
The value of i to the power of 45 is i.
just ask! I am your teacher!
Your "bell work" code as written will not print out anything, as the code will fail to compile. The first occurrence of the variable number3 appears as "number 3". Also, the line
is not a legit for-loop. I'm guessing you were going for "i <= n".
Correcting the syntax errors would result in an output of 11235813, the first 7 numbers of the Fibonacci sequence in run-on form.
Excellent work doughtaker! A+ work for sure. Corrected the mistakes + found the Fibonacci sequence. You would win more sbd, but I've been transferring the sbd payouts to btc. Maybe I'll make another contest for steem. Nice hearing from you!
Congratulations @hansenator! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published 4 posts in one day
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP