If all the code snippets below assume properly that an ...
IMPORTANT CONCEPTS TO UNDERSTAND in JAVA:
(answer key)
If all the code snippets below assume properly that an array, myArray, has been properly instantiated then;
How to average correctly
int x, y, sum;
double average;
sum = x + y;
average = (double) sum / 2;
// the key here is to cast the sum to a double.
How to average correctly in an array
int x, y, sum;
double average;
for (int i = 0; i < myArray.length; i++)
sum +- myArray[i];
average = (double) sum / myArray.length;
// the key here is to cast the sum to a double to not lose decimal data
How to find max/min in an array
int max, min;
max = myArray[0] ;
min = myArray[0];
for (int i = 0; i < myArray.length; i++)
{
if (myArray[i] > max)
max = myArray[i];
if (myArray[i] < min)
min = myArray[i];
}
// the key here is to properly initialize max and min before entering the for loop
How to swap elements
int x, y, temp;
temp = x;
x = y;
y = temp;
// the key here is to perform the swap in the correct order
How to swap elements in an array
int x, y, temp;
temp = myArray[thisIndex];
myArray[thisIndex] = myArray[thatIndex];
myArray[thatIndex] = temp ;
// the key here is to perform the swap in the correct order
How to reverse an array
int x, y, temp;
for (int i = 0; i < myArray.length / 2; i++)
{
temp = myArray[i];
myArray[i] = myArray[myArray.length – 1 - i];
myArray[myArray.length – 1 - i] = temp ;
}
// the key here is to perform the swap in the correct order through just half of the array
How to increase the size of an array
int count;
if (count = = myArray.length)
{
myArrayType[] temp = new myArrayType[myArray.length * 2];
for (int i = 0; i < myArray.length; i++)
temp[i] = myArray[i];
myArray = temp;
}
Explain the selection sort/insertion sort algorithms
This is outlined very well in your text book!
How to tell if a number is odd or even
int x;
boolean isEven = false;
if (x % 2 = = 0)
isEven = true;
// otherwise isEven returns false
How to turn a number into a String
int x;
String y;
X = 125;
y = “ “ + 125; // do not attempt to cast the number into a String!!!
How to prompt users for input. What lines are necessary to “scan” for input?
// First, you must make the scanner class available…
import java.util.Scanner;
public class MyJavaProgram
{
// Then you must initialize the scanner object
Scanner scan = new Scanner(System.in);
String message;
// Finally, you must prompt for input
System.out.println(“Please enter info: “);
message = scan.next(); // use scan.next() and scan.nextline() for Strings,
// use scan.nextInt(); for integers!
How to compute/generate a random number.
// HINT: you will need to use the math class to do this.
int num1;
double num2;
// First, generate random integer from 0 to 9
num1 = (int)(Math.random() * 10);
System.out.println ("From 0 to 9: " + num1);
// Second, generate random integer from 1 to 10
num1 = (int)(Math.random() * 10 + 1);
System.out.println ("From 1 to 10: " + num1);
// Third, generate random integer from 20 to 34
num1 = (int)(Math.random() * 15 + 20);
System.out.println ("From 20 to 34: " + num1);
// Fourth, generate random decimal from 0 to 0.99
num2 = Math.random();
System.out.println ("A random double [between 0-1]: " + num2);
// Fifth, generate random decimal from 0 to 5.99
num2 = Math.random() * 6;
System.out.println ("A random double [between 0-6]: " + num2);
How to extract individual digits from an integer.
// HINT: the concept is called modulus and you will need to use % operator(remainder)
int num = 12345;
int digitNum; // will be the extracted number
// get the ones digit
digitNum = num % 10; // returns 5
// get the tens digit
// to get the to the tens digit, we first divide by 10 (12345/10=1234)
num /= 10;
// now we can compute the remainder
digitNum = num % 10; // returns 4
// get the hundreds digit
// to get the to the hundreds digit, we first divide by 100 (12345/100=123)
num /= 100;
// now we can compute the remainder
digitNum = num % 10; // returns 3
// etc.
…
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java swing first programs ece cis
- the university of tennessee at chattanooga university
- arraylists school of computing and information sciences
- exercises computer science department
- client server tutorial
- if all the code snippets below assume properly that an
- reference manual columbia university
- multi dimensional arrays
Related searches
- how to properly start an essay
- are the functions given below exponential functions
- all the prayers in the bible pdf
- how to properly acknowledge an email
- all the words in the english language
- all the words in the world list
- all the words in the world
- all the phases of the moon
- all the events that happened in 2020
- how to properly cite an article apa
- all the things that happened in 2020
- how to properly structure an essay