General

Tech

Coding Prompts for Java

Salutations.
I am currently taking AP Computer Science at my high school and I could use some practice. As you can see in the title, I am learning Java.
At the moment, I know how to use:
For Loops
Do/while loops
How to cast a variable
Arrays [a bit shaky]
Methods [currently learning]

Would you guys mind giving me a prompt to work on? This class is somewhat difficult as I am completely new to the coding world.

November 13, 2014

14 Comments • Newest first

hairamoose

A good skill to learn NOW is how to trace through your code and don't just trace through your head. WRITE IT DOWN. It helps a lot and on what ever language it is tracing will always work. Also there's a neat feature in eclipse called the debugger which essentially traces your program for you!

Edit: Also the code above me it's because you are trying to access array at position 1. But where is position 1 in a 2-d array? Also i'm not 100% sure but the data doesn't look right either

Reply November 15, 2014 - edited
MicroMyCrow

[quote=prosecuted]Okay I see, and I can assume that multiple values can not be placed in the same line of code (similar to 1D Arrays)?[/quote]

Ok, I was unsure, so I opened eclipse.
Apparently you can't do:

public class example {
public static void main(String[] args) {
int array[][] = new int [2][3];
array[1] = [1,2,3]; //<< Problem is here, eclipse won't compile this.
System.out.println(array[1][0]);
}
}

Sweet, alright.
I will be off to bed, it's late and if you do post your answer to my question, ill check it out tomorrow.

Reply November 13, 2014 - edited
prosecuted

[quote=MicroMyCrow]Ahh I forget to mention that.
Ok let me start over.

int 1D = new int[8];
we can think of a chess board or a matrix of size A X B;
Let's choose a chess board, that will mean a 8 X 8;

inside this newly initialized array "1D"
1D is a row of data.
1D = [0,1,2,3,4,5,6,7]

In a 2D array we can think of it as
int row = 2;
int column = 3;
int 2D = new int[row][column];

// 2D can hold 2 rows of Arrays, each Array will have 2 columns in each array,
//we get the number of elements by multiplying them.
that is why we can store 6 elements in our previous example.

int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]
--------------------
[b]"Wouldn't 2Darray length be [0,1] and the next would be [2,3,4]?"[/b]

Hopefully you see now why.
------------------------
Maybe an image will help.
http://www.tutorialspoint.com/images/two_dimensional_arrays.jpg

Using the same example:
int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]

we can rearrange the array to look like this
ROW 0 = [0, 1, 2]
ROW 1 = [3, 4, 5]

if we wanted to access the cell storing 5
we would call:
2DArray[1][2]
----
[b]"Actually, the values are a bit difficult to appropriately place,,
int arrayV5[][];
arrayV5 = new int [2][3];
arrayV5[[2][3]] = [[30, 15, 15] [15,15, 15]];"[/b]

Not sure of what you mean, also it should be arrayV5[2][3]
If i understand the problem,
we can return the 1D arrays from the array by only calling the rows
arrayV5[0] --> [30, 15, 15]

if we wanted to call on a particular element from that row.
arrayV5[0][1] --> [15][/quote]

Okay I see, and I can assume that multiple values can not be placed in the same line of code (similar to 1D Arrays)?

**Edit**
Yes I was able to get what you said.. (0,2,4..)
Thank you so much for the help! 2 Dimensional arrays make way more sense, and double nested FOR loops are simpler too!

Reply November 13, 2014 - edited
MicroMyCrow

[quote=prosecuted]I think i might be able to implement it.. though it is still a bit confusing.
A question though, your statement: int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]
Wouldn't 2Darray length be [0,1] and the next would be [2,3,4]?
Since you are giving the lengths of 2 and 3 respectively?[/quote]

Ahh I forget to mention that.
Ok let me start over.

int 1D = new int[8];
we can think of a chess board or a matrix of size A X B;
Let's choose a chess board, that will mean a 8 X 8;

inside this newly initialized array "1D"
1D is a row of data.
1D = [0,1,2,3,4,5,6,7]

In a 2D array we can think of it as
int row = 2;
int column = 3;
int 2D = new int[row][column];

// 2D can hold 2 rows of Arrays, each Array will have 2 columns in each array,
//we get the number of elements by multiplying them.
that is why we can store 6 elements in our previous example.

int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]
--------------------
[b]"Wouldn't 2Darray length be [0,1] and the next would be [2,3,4]?"[/b]

Hopefully you see now why.
------------------------
Maybe an image will help.
http://www.tutorialspoint.com/images/two_dimensional_arrays.jpg

Using the same example:
int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]

we can rearrange the array to look like this
ROW 0 = [0, 1, 2]
ROW 1 = [3, 4, 5]

if we wanted to access the cell storing 5
we would call:
2DArray[1][2]
----
[b]"Actually, the values are a bit difficult to appropriately place,,
int arrayV5[][];
arrayV5 = new int [2][3];
arrayV5[[2][3]] = [[30, 15, 15] [15,15, 15]];"[/b]

Not sure of what you mean, also it should be arrayV5[2][3]
If i understand the problem,
we can return the 1D arrays from the array by only calling the rows
arrayV5[0] --> [30, 15, 15]

if we wanted to call on a particular element from that row.
arrayV5[0][1] --> [15]

-----------------------------------------------------------------------------------------
for a 2D array, a simple method is a nested FOR loop, hopefully might give you an idea how to implement my problem,
but looks like you are getting the idea.

int arrayV5[][] = new int [2][3];
int i = 0;
for( int row = 0; row < 2; row++){
_for(int column = 0; column < 3; column++){
__arrayV5[row][column] = i
__i+=2;
_}
}

what we will get back is
arrayV5 = [ [0,2, 4], [6,8,10] ]

Reply November 13, 2014 - edited
prosecuted

[quote=MicroMyCrow]oh sorry.
Arrays can have multiple dimensions
I am sure you are familiar with 1-Dimensions
int 1DArray = new int[6]
1DArray = [0,1,2,3,4,5]

if we print this out, it will be represented like a list,
0 1 2 3 4 5

In 2-Dimensions (2D) arrays,
int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]

we can represent them like this:
0 1 2
3 4 5

Both these arrays hold 6 elements, it's just different ways of storing the data.
We can interpret a 2D array as a 1D array where each element, in this case 2 is another 1D array, so an Array holding an Array in each element.
1DArrayA[0] = 1DarrayB[0,1,2];
1DArrayA[1] = 1DarrayC[3,4,5];

so we have three arrays,
Our main Array is 1DArrayA in it's first element [0] we are storing 1DArrayB[] inside of it
And in 1DArrayA's 2nd element [1] we are storing 1DArrayC[] inside of it.

Therefore 1DArrayA[] is equivalent to 2DArray[][] in the amount of data it can store.[/quote]
I think i might be able to implement it.. though it is still a bit confusing.
A question though, your statement: int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]
Wouldn't 2Darray length be [0,1] and the next would be [2,3,4]?
Since you are giving the lengths of 2 and 3 respectively?
******Edit*********
Ah i see my mistake, 2 and 3 are the dimensions.
I am starting to understand it.

********Edit*********
Actually, the values are a bit difficult to appropriately place,,

int arrayV5[][];
arrayV5 = new int [2][3];
arrayV5[[2][3]] = [[30, 15, 15] [15,15, 15]];

Reply November 13, 2014 - edited
MicroMyCrow

oh sorry.
Arrays can have multiple dimensions
I am sure you are familiar with 1-Dimensions
int 1DArray = new int[6]
1DArray = [0,1,2,3,4,5]

if we print this out, it will be represented like a list,
0 1 2 3 4 5
-------------------

In 2-Dimensions (2D) arrays,
int 2DArray = new int[2][3]
2Darray = [ [0, 1, 2][b],[/b] [3, 4, 5] ]

we can represent them like this:
0 1 2
3 4 5
-----------------------

Both these arrays hold 6 elements, it's just different ways of storing the data.
We can interpret a 2D array as if it was a 1D array where each element in the array, in this case 2, is storing a 1D array.
So an Array holding an Array in each element.
int 1DArrayA = new int [2];
int 1DarrayB = new int[3];
int 1DarrayC = new int[3]

1DArrayA[0] = 1DarrayB[0,1,2];
1DArrayA[1] = 1DarrayC[3,4,5];

so we have three arrays,
Our main Array is 1DArrayA in it's first element [0] we are storing 1DArrayB[] inside of it
And in 1DArrayA's 2nd element [1] we are storing 1DArrayC[] inside of it.

Therefore 1DArrayA[] is equivalent to 2DArray[][] in the amount of data it can store.

Reply November 13, 2014 - edited
prosecuted

[quote=MicroMyCrow]Are you looking for something like:
Using a 1-Dimension array of N elements, iterate through this as if it was a 2-D array using a double nested FOR loop.
Using a WHILE loop,you must first populate the array from 0,1,2,3,...(N-1).

You must use in your code
*double N = X; //X is some value you will hardcode and cast to an int,
*Make the FOR and WHILE loop in separate methods.
Example Method:
int populate_Array(int Array, int N){
//Something missing here.
_while(......//Something missing here too){
__array[//Some variable] = something
__//something to iterate
_}
return array;
}

*I guess this is also simple but print out your result to console as a 2-D array using System.out.println();

I tried to use everything you gave me on your list.[/quote]
What exactly is a 2-d array? :c
This:?

x 5
y 6
z 7

Thanks for the idea! I will definitely try it out
@Masinko
I actually received a similar prompt from my father, he wanted me to calculate the OBS (OPS?) for baseball players, so i wrote..

import cs1.*;
//Name
//Prompt1
//Calculate OPS
//Start Date: November 12, 2014
//End Date: ?
public class Prompt1 {
public static void main (String [] args){
double slug[], bat[], slugAvg, batAvg, OPS;
String name[];
int players, info, persistence;
boolean foundIt;
System.out.println("How many players will you include?"
players = Keyboard.readInt();
slug = new double [players];
bat = new double [players];
name = new String [players];
for (int x = 0; x < players; x++){
System.out.println("Please input the player's name, slug average, and batting average."
name[x] = Keyboard.readString();
slug[x] = Keyboard.readDouble();
bat[x] = Keyboard.readDouble();
}
do {
System.out.println("Which number player would you like to know about?"
info = Keyboard.readInt();
System.out.println("His name is " + name[info]);
System.out.println("His slug average is " +slug[info]);
System.out.println("His batting average is "+ bat[info]);
OPS = bat[info] + slug[info];
System.out.println("His OPS is " + OPS);
System.out.println();
System.out.println("Would you like to know about another athlete?"
System.out.println("Press 0 to stop or 1 to continue."
persistence = Keyboard.readInt();
if (persistence == 1)
foundIt = true;
else foundIt = false;
} while (foundIt = true);
}
}

Reply November 13, 2014 - edited
Masinko

Well, if your arrays are a bit shaky, you could try making a program that initializes two arrays (however you want it to), and try multiplying them together.

Since you're learning methods and what not, I'm assuming this includes passing objects into methods, so you could try to make a separate multiplier method that multiplies the values of the arrays there rather than a loop in your main method that does it for you.

Reply November 13, 2014 - edited
MicroMyCrow

Are you looking for something like:
Using a 1-Dimension array of N elements, iterate through this as if it was a 2-D array using a double nested FOR loop.
Using a WHILE loop,you must first populate the array from 0,1,2,3,...(N-1).

You must use in your code
*double N = X; //X is some value you will hardcode and cast to an int,
*Make the FOR and WHILE loop in separate methods.
Example Method:
int populate_Array(int Array, int N){
//Something missing here.
_while(......//Something missing here too){
__array[//Some variable] = something
__//something to iterate
_}
return array;
}

*I guess this is also simple but print out your result to console as a 2-D array using System.out.print();
//You can use "n" to get a new line if you concatenate it to a string.

I tried to use everything you gave me on your list.

Reply November 13, 2014 - edited
Avatar

Welp ignored

Reply November 13, 2014 - edited
prosecuted

[quote=Avatar]I forgot everything I knew about java after I finished my course because I learned C right after... Can you call addresses in java?[/quote]
I think that is an Array?
I'm not sure man.. I'm only in the second quarter of this class.
All I really need is a prompt to try to solve is all.

Reply November 13, 2014 - edited
Avatar

I forgot everything I knew about java after I finished my course because I learned C right after... Can you call addresses in java?

Reply November 13, 2014 - edited
prosecuted

[quote=djmaxaaron]Someone explain for loops and while loops for C++ thanks [/quote]

I caught a glimpse of C# today and it looks very similar, the words are just slightly off.
What do you have to go through for a FOR LOOP?

Reply November 13, 2014 - edited
djmaxaaron

Someone explain for loops and while loops for C++ thanks

Reply November 13, 2014 - edited