Hello guys, I need an explanation for the study guide to help me to understand :
ID: 3793856 • Letter: H
Question
Hello guys, I need an explanation for the study guide to help me to understand :
Study Guide
Know:
1. Two ways to instantiate an array:
int[] myInts = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
and
int[] myInts = new int[100];
2. Two ways to traverse an array:
for (int i = 0; i < myInts.length; i++)
{
myInts[i] = i + 1;
}
and
for (int myValue : myInts)
{
System.out.println(myValue);
}
3. Be able to trace through some looping structures and determine what the output/effect would be:
int x = 2;
int y = 5;
while (x >= 5)
{
System.out.print(x * y + " ");
x--;
}
4. Nested loop:
for (x = 1; x <= 5; x++)
{
for ( y = 0; y <= x; y++ )
{
System.out.print(x * y + " ");
}
System.out.println();
}
5. Conditionals:
public static void main(String[] args)
{
int x;
int y;
Scanner kb = new Scanner(System.in);
System.out.println("Enter two integers for x and y: ");
x = kb.nextInt();
y = kb.nextInt();
if (x > 0 && y > 0)
{
System.out.print("You are");
if (x > 10)
System.out.print(" way cool"); // <-No braces required if only one statement...
else //
if (y < 10)
System.out.print(" crazy");
else //
System.out.print(" man");
}
else //
{
if (x < 0)
{
System.out.print("We are");
}
else //
{ if (y < 0)
System.out.print("They are");
else //
System.out.print(" the best");
}
System.out.print(" OK");
}
System.out.println(", if you know what I mean.");
}
x: y: You are crazy, if you know what I mean.
x: y: We are OK, if you know what I mean.
x: y: We are the best OK, if you know what I mean.
x: y: You are the best OK, if you know what I mean.
x: y: the best OK, if you know what I mean.
x: y: OK, if you know what I mean.
x: y: You are way cool, if you know what I mean.
x: y: They are the best OK, if you know what I mean
x: y: They are OK, if you know what I mean
and
‘Complete the following:’
Scanner kb = new Scanner(System.in);
int choice = 0;
do{
System.out.println(" Please choose ");
System.out.println("1. Do this");
System.out.println("2. Do that");
System.out.println("3. Do the other thing");
System.out.println("4. Quit");
System.out.print("Enter option--> ");
choice = kb.nextInt();
kb.nextLine();
switch (_________)
{
___________:
System.out.print("You selected option 1");
____________;
___________:
System.out.print("You selected option 2");
____________;
___________:
System.out.print("You selected option 3");
____________;
___________:
System.exit(0);
}
}while (true);
6. String class. Be able to substring, isolate individual characters and to compare two Strings.
String myString = “Hello”;
System.out.println(myString.charAt(1));
System.out.println(myString.substring(1, 3));
System.out.println(myString.substring(1));
If (myString.equals(yourString))
Thank you...
Explanation / Answer
1. Two ways to instantiate an array:
int[] myInts = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
// myInts is an array of 10 integers whose values are initialized---declaration and initialization
int[] myInts = new int[100];
// myInts is an array of 100 integers --- declaration only not initialization
2. Two ways to traverse an array:
for (int i = 0; i < myInts.length; i++)
{
myInts[i] = i + 1;
}
// myInts[0] = 1, myInts[1] = 2, myInts[2] = 3......................
for (int myValue : myInts)
{
System.out.println(myValue); //display the values of myInts
}
3. Be able to trace through some looping structures and determine what the output/effect would be:
int x = 2;
int y = 5;
while (x >= 5)
{
System.out.print(x * y + " ");
x--;
}
Output:
No Output as While condition is false (x = 2 <5) so while wil never execute and nothing will be displayed .
4. Nested loop:
for (x = 1; x <= 5; x++) // 5 rows
{
for ( y = 0; y <= x; y++ ) // for each row it will display x columns starting from 0 to x with each value = x*y
{
System.out.print(x * y + " ");
}
System.out.println();
}
output:
5. Conditionals:
public static void main(String[] args)
{
int x;
int y;
Scanner kb = new Scanner(System.in);
System.out.println("Enter two integers for x and y: ");
x = kb.nextInt();
y = kb.nextInt();
if (x > 0 && y > 0)
{
System.out.print("You are");
if (x > 10)
System.out.print(" way cool"); // <-No braces required if only one statement...
else //
if (y < 10)
System.out.print(" crazy");
else //
System.out.print(" man");
}
else //
{
if (x < 0)
{
System.out.print("We are");
}
else //
{ if (y < 0)
System.out.print("They are");
else //
System.out.print(" the best");
}
System.out.print(" OK");
}
System.out.println(", if you know what I mean.");
}
x: >0 y: >0,y<10 You are crazy, if you know what I mean.
x: <0 , y: >0 We are OK, if you know what I mean.
x: <0 y: >0 We are the best OK, if you know what I mean.
x: >0 y: >0 You are the best OK, if you know what I mean.
x: <0 y: >0 the best OK, if you know what I mean.
x: <0 y: <0 OK, if you know what I mean.
x: >10 y: >0 You are way cool, if you know what I mean.
x: >0 y: >0 They are the best OK, if you know what I mean
x: >0 y: <0 They are OK, if you know what I mean
and
‘Complete the following:’
Scanner kb = new Scanner(System.in);
int choice = 0;
do{
System.out.println(" Please choose ");
System.out.println("1. Do this");
System.out.println("2. Do that");
System.out.println("3. Do the other thing");
System.out.println("4. Quit");
System.out.print("Enter option--> ");
choice = kb.nextInt();
kb.nextLine();
switch (choice)
{
case 1:
System.out.print("You selected option 1");
break;
case 2:
System.out.print("You selected option 2");
break;
case 3:
System.out.print("You selected option 3");
break;
case 4:
System.exit(0);
}
}while (true);
6. String class. Be able to substring, isolate individual characters and to compare two Strings.
import java.util.*;
class myString
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String myString = "Hello";
for(int i=0;i< myString.length();i++)
System.out.println(myString.charAt(i));
//System.out.println(myString.substring(1, 3));
//System.out.println(myString.substring(1));
System.out.println("Enter your String");
String yourString = scan.next();
if (myString.equals(yourString))
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.