Printing from a method - java

public class Simple2DArray {
public static void main(String[] args) {
}
public static void fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;r++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
}
public static int biggest(int[][] grid, int big)
{
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;r++)
{
if(grid[r][c]> big)
{
big = grid[r][c];
}
}
}
System.out.println(big);
return big;
}
}
I would like for it to print big. How do I do this? I tried putting System.out.println(biggest(null,big) in the main method but that did not work.

I got solution
But first of all go through https://stackoverflow.com/help/how-to-ask
Your solution
I made some changes in your code.
public class Simple2DArray {
public static void main(String[] args) {
int[][] grid = fill();
System.out.println(biggest(grid, 20));
}
public static int[][] fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
System.out.println("Input 2D");
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
System.out.print(grid[r][c]);
System.out.print("\t");
}
System.out.println("");
}
return grid;
}
public static int biggest(int[][] grid, int big)
{
if (grid != null) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] > big) {
big = grid[r][c];
}
}
}
}
return big;
}
}

The following corrections need to be made:
fill needs to return grid.
biggest doesn't need big as a parameter (it makes little sense). It should start out as Integer.MIN_VALUE.
There's no need to print big within biggest.
In your for loop that loops through values of c, you should have c++ instead of r++.
public static int[][] fill()
{
int[][] grid = new int[5][5];
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
grid[r][c] = (int)(Math.random()*100);
}
}
return grid;
}
public static int biggest(int[][] grid)
{
int big = Integer.MIN_VALUE;
for(int r=0;r<grid.length;r++)
{
for(int c=0;c<grid[r].length;c++)
{
if(grid[r][c]> big)
{
big = grid[r][c];
}
}
}
return big;
}
Then you can call biggest on an actual two-dimensional array (null isn't an actual two-dimensional array):
public static void main(String[] args)
{
int[][] array = fill();
System.out.println(biggest(array));
}

Related

Java. Array, save previous values

Cant figure out how to do this thing. I need to store and count number of "jumps" and direction of "jump" in array.
Console has to display something like this:
: Jump 1, direction Left
: Jump 2, direction Right
In my code below, when I print array elements, int jump always equals to 0;
I tried many variations, sometimes I got (for example):
jump();
jump();
jump();
jump();
And it prints: {0,0,0,4} , instead of {1,2,3,4};
public class Jump {
int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;
public void jump() {
jump++;
if (jump > 50) {
System.out.println("need a rest");
return;
} else {
for (int i = 0; i < jumpArray.length; i++) {
jumpArray[i] = new Jump();
}
}
jumpArray[jump].storeJump = jump;
jumpArray[jump].storeDirection = direction;
}
public static void main(String[] args) {
Jump jumper = new Jump();
jumper.jump();
jumper.jump();
for (int i = 0; i < jumper.jump; i++) {
System.out.println(jumper.jumpArray[i].jump);
}
}
}
I think the code below will solve your problem, but I think You should improve your code.
int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;
public void jumping() {
jump++;
if (jump > 50) {
System.out.println("need a rest");
return;
} else {
jumpArray[jump] = new Jump();
}
jumpArray[jump].storeJump = jump;
jumpArray[jump].storeDirection = direction;
}
public static void main(String[] args) {
Jump jumper = new Jump();
jumper.jumping();
jumper.jumping();
Set<Jump> mySet = new HashSet<Jump>(Arrays.asList(jumper.jumpArray));
for (int i = 1; i < mySet.size(); i++) {
System.out.println(jumper.jumpArray[i].storeJump);
}
}
I think it would make more sense to move the jumpArray into main():
public class Jump {
String direction;
//int storeJump; Not sure what this is for
//String storeDirection; Or this
#Override
public String toString() {
return direction;
}
// Static function that creates an initializes a Jump object
public static Jump jump(String direction) {
Jump jump = new Jump();
jump.direction = direction;
return jump;
}
public static void main (String[] args)
{
Jump[] jumpArray = new Jump[100];
int numJumps = 0;
// A list of directions for testing
String [] path = new String[]{"north", "south", "east", "west"};
// Create a bunch of jumps
for (String dir : path) {
if (numJumps > 50) {
System.out.println("need a rest");
break;
}
// Create a new jump and add to the array
jumpArray[numJumps++] = Jump.jump(dir);
}
// Print the jumps
for (int i = 0; i < numJumps; i++) {
System.out.println(i + " " + jumpArray[i]);
}
}
}

Using Different Methods for Input and Display of an Array

public class InputRealNums1
{
static double [] numArr = new double [(int) (Math.random()*100)];
static String res = "";
public static void main(String[] args)
{
inputArray();
displayArray();
}
static void inputArray()
{
for (int i = 0 ; i < numArr.length ; i++)
{
numArr[i] = Math.random()*100;
}
}
static void displayArray()
{
System.out.println(res.inputArray());
}
}
The aim of this code is to seperate the input and the output. The program would generate certain numbers and then it will display them. I just want to know how to seperate the the input and output with the above methods.
You can display that array by using this method.
public static void displayArray()
{
for (int i = 0 ; i < numArr.length ; i++)
{
System.out.println(numArr[i]);
}
}
But this is not a good way. You can do this by passing parameters to the method.
public static void displayArray(Double[] myArray)
{
for (int i = 0 ; i < myArray.length ; i++)
{
System.out.println(myArray[i]);
}
}
public static void main(String[] args){
// some code in here
displayArray(numArr);
}

Bubble Sort an Array

I have an add point method, which adds a slot and puts a point inside. After adding the slot I want to sort what is in the array. How do I do that? Here is my code:
public void add(Point p)
{
int k;
for (int j = 0;j<myConsumerCurve.length;j++)
for (k=j+1;k<myConsumerCurve.length;k++)
if (k!=j && myConsumerCurve[k] == myConsumerCurve[j]) {
}
Point[] tempCurve = new Point [myConsumerCurve.length +1];
for(int i=0; i < myConsumerCurve.length; i++)
{
tempCurve[i] = myConsumerCurve[i];
}
tempCurve[myConsumerCurve.length] = p;
myConsumerCurve = tempCurve;
bubblesort(myConsumerCurve);
}
public static void bubblesort(Point[] myConsumerCurve)
{
}

Get numbers that have only 2,3 and 5 as prime factors

I am writing a code to find numbers which have prime factor 2,3 or 5.Problem is that after testing the value which have prime factors 2,3 or 5, I am not able to put the value in an array array[j] without any space(i.e null value).Do you have any suggestion for this?I have marked the problem area.
public class test{
static int search(int i)
{
for(int n=1;n<=Math.sqrt(i);n++){
if(i==1){
System.out.println(i);
}
else if(i%2==0){
i=i/2;
System.out.println(i);
}
else if (i%3==0){
i=i/3;
System.out.println(i);
}
else if (i%5==0){
i=i/5;
System.out.println(i);
}
}
return i;
}
public static void main(String[] args) {
int array[]= new int[100];
{
for(int i=1;i<100;i++)
{
int m;
m=search(i);
if(m==1||m==2 || m==3 || m==5){
for(int j=i;j<=i;j++)
array[j]=i;//Problem is here
}
}
}
}
}
public static void main(String[] args) {
int array[]= new int[100];
{
int index=0;
for(int i=0;i<100;i++)
{
int m;
m=search(i);
if(m==1||m==2 || m==3 || m==5){
array[index++]=i;
}
}
}}
Have a look.i hope it will help.
public static void main(String[] args) {
int array[]= new int[100];
{
int index=1;
for(int i=1;i<100;i++)
{
int m;
m=search(i);
if(m==1||m==2 || m==3 || m==5){
array[++index]=i;
}
}
}}
Try this please.

Why does the first 8 elements of the array in this quicksort code change into 1,2,3,4,5,6,7,8?

for some reason, the code prints out:
1
2
3
4
5
6
7
8
sortedNum
sortedNum
after getting sorted and I cannot seem to find the error. This happens every time, and as I can see, the ArrayList does not contain the numbers 1-8 every time.
import java.util.*;
public class QuickSortRunner {
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int n = 0; n<10;n++)
{
int x = (int)(Math.random()*101)+1;
arr.add(x);
}
for(int x=0;x<10;x++)
{
System.out.println(arr.get(x));
}
//int size = arr.size();
partition(arr,0,arr.size()-1);
for(int x=0;x<10;x++)
{
System.out.println(arr.get(x));
}
}
public static void partition(ArrayList<Integer> arr, int lower, int upper)
{
int u=upper, l=lower;
if(u<l)
{
return;
}
int rand = (int)(Math.random()*(upper-lower)+lower);
int pivot = arr.get(rand);
while(l<=u)
{
while(arr.get(l)<pivot)
{
l = l+1;
}
while(arr.get(u)>pivot)
{
u=u-1;
}
if(l<=u)
{
int temp = arr.get(l);
arr.set(l,u);
arr.set(u,temp);
l++;
u--;
}
if(lower<u)
{
partition(arr,lower,u);
}
if(l<upper)
partition(arr,l,upper);
}
}
}
I'm not sure if this is the only problem, but one problem is that
arr.set(l,u);
is supposed to be
arr.set(l, arr.get(u));

Categories

Resources