"Here are a few examples of interesting things you can do with arrays:"
Example 1.
Fill a 10-element array with numbers from 1 to 10:
publicclassMainClass{publicstaticvoidmain(String[] args){int[] numbers =newint[10];for(int i =0; i < numbers.length; i++){
numbers[i]= i +1;}}}
Fill a 10-element array with numbers from 10 to 1:
publicclassMainClass{publicstaticvoidmain(String[] args){int[] numbers =newint[10];for(int i =0; i < numbers.length; i++){
numbers[i]=10- i;}}}
Fill a 10-element array with numbers from 0 to 9:
publicclassMainClass{publicstaticvoidmain(String[] args){int[] numbers =newint[10];for(int i =0; i < numbers.length; i++){
numbers[i]= i;}}}
Fill a 10-element array with numbers from 9 to 0:
publicclassMainClass{publicstaticvoidmain(String[] args){int[] numbers =newint[10];for(int i =0; i < numbers.length; i++){
numbers[i]=9- i;}}}
Example 2.
Read 10 strings from the keyboard:
publicclassMainClass{publicstaticvoidmain(String[] args)throwsIOException{BufferedReader reader =newBufferedReader(newInputStreamReader(System.in));String[] list =newString[10];for(int i =0; i < list.length; i++){
list[i]= reader.readLine();}}}
Read 10 numbers from the keyboard:
publicclassMainClass{publicstaticvoidmain(String[] args)throwsIOException{BufferedReader reader =newBufferedReader(newInputStreamReader(System.in));int[] list =newint[10];for(int i =0; i < list.length; i++){String s = reader.readLine();
list[i]=Integer.parseInt(s);}}}
Example 3.
Display an array on the screen:
publicclassMainClass{publicstaticvoidmain(String[] args)throwsIOException{int[] list =newint[10];// Fill the arrayfor(int i =0; i < list.length; i++)
list[i]= i;// Display the contents of the arrayfor(int i =0; i < list.length; i++)System.out.println(list[i]);}}
Example 4.
Quick (static) initialization. Add up the array elements:
publicclassMainClass{publicstaticvoidmain(String[] args)throwsIOException{// Static initializationint[] list ={5,6,7,8,1,2,5,-7,-9,2,0};// Calculate the sumint sum =0;for(int i =0; i < list.length; i++)
sum += list[i];System.out.println("Sum is "+ sum);}}
Example 5.
Find the smallest element in an array:
publicclassMainClass{publicstaticvoidmain(String[] args)throwsIOException{int[] list ={5,6,7,8,1,2,5,-7,-9,2,0};int min = list[0];for(int i =1; i < list.length; i++){if(list[i]< min)
min = list[i];}System.out.println ("Min is "+ min);}}
This is the signIn() method that greets website users. Currently, it greets all users, but it should only greet registered users. All unregistered users have the name "user".
Add username validation at the beginning of the signIn() method.
If the name is "user", use the return keyword to abort execu
3
Task
Module 1. Java Syntax, level 9, lesson 2
Locked
A cube calculator
Let's implement a calculator that will compute the powers of numbers.
To do this, create a cube() method. It must take a long integer value as an argument.
The method should raise the passed value to the third power and return it as the method's result.
The numbers you have to work with may be large
9
Task
Module 1. Java Syntax, level 9, lesson 2
Locked
A cubed cube calculator
You won't blow any minds by unveiling a calculator that can raise a number to the power of three.
But a calculator that can raise a number to the power of nine is another matter! So let's implement one!
To do this, create a public static long ninthDegree(long) method.
It must take a long integer val
Hint: what does NULL do when it appears that only 8 element slots are taken in a 10 space Array ?
Hint2: build from the examples about and then step thru the output possibilities
if you got this far is obvious that you are capable to do anything you put in your mind. Is not easy most of the times but in case to grow we need to go through discomfort. Keep it up you doing amazing job.. You are unstoppable ..
Guys
My IDEA reading the code in "Read 10 strings from the keyboard:" as an error in the line :
list[i] = reader.readLine();
Could you explain me why? Does anybody have the same problem?
public static void main(String[] args) throws IOException
you needs have ( throws IOException ) which inturn imports (import java.io.IOException;) if you are using intellij IDE