Jump to content

{JAVA} How to create a method to find mode?

creativename

I need my code to create an array of 10 random integers, and then I need to use a new method to find the mode of those 10 integers.

 

My code so far is

 

 

 public class mode{public static void main(String args[]){int[] x;x = new int[10];System.out.print("Data Array: ");for(int count = 0; count<x.length; count++){x[count] = (int) (Math.random()*10);System.out.print(x[count] + " ");}}}
 

 

 

I'm not quite sure how to create a new method, or how to find mode.

Edited by alpenwasser
code tags ;)

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

 

I'm not quite sure how to create a new method, or how to find mode.

What are y and z? 

 

Do you have to create it as a method or can it be in the same function? 

 

And does efficient matter? 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

What are y and z? 

 

Do you have to create it as a method or can it be in the same function? 

Oh it seems those are the left over variables from my other assignment where I had to talley the array. Let me edit it out.

 

And the goal was to create a method that finds mode.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

Oh it seems those are the left over variables from my other assignment where I had to talley the array. Let me edit it out.

 

And the goal was to create a method that finds mode.

 

This could be your method: and then inside the brackets you would implement it, and then return either the index to the most common element, or the value of the most common element. 

int findMode(int[] array){ }

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

This could be your method: and then inside the brackets you would implement it, and then return either the index to the most common element, or the value of the most common element. 

int findMode(int[] array){ }

I don't have to do that "public void findMode" thing?

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

I don't have to do that "public void findMode" thing?

my bad

public static int findMode(int[] array){ }

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

my bad

public static int findMode(int[] array){ }

 

 

I got these errors

 

mode.java:12: error: illegal start of expression

public int findMode(int[] array);

^

mode.java:12: error: ';' expected

public int findMode(int[] array);

                   ^

mode.java:12: error: ';' expected

public int findMode(int[] array);

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

I got these errors

 

mode.java:12: error: illegal start of expression

public int findMode(int[] array);

^

mode.java:12: error: ';' expected

public int findMode(int[] array);

                   ^

mode.java:12: error: ';' expected

public int findMode(int[] array);

lose the semicolon

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

lose the semicolon

It gave even more errors lol

mode.java:12: error: illegal start of expression

public int findMode(int[] array)

^

mode.java:12: error: ';' expected

public int findMode(int[] array)

                   ^

mode.java:12: error: ';' expected

public int findMode(int[] array)

                               ^

mode.java:14: error: ';' expected

System.out.print(x.findMode)

                                     ^

4 errors

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

 

I need my code to create an array of 10 random integers, and then I need to use a new method to find the mode of those 10 integers.

 

My code so far is

 

 

 

public class mode
{
     public static void main(String args[])
     {
         int[] x;
         x = new int[10];
         System.out.print("Data Array: ");
        for(int count = 0; count<x.length; count++)
          {
        x[count] = (int) (Math.random()*10);
          System.out.print(findMode(x));
        }
    }
    public static int findMode(int[] array){

  

    }
}
 
 
 
I'm not quite sure how to create a new method, or how to find mode.

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

I need my code to create an array of 10 random integers, and then I need to use a new method to find the mode of those 10 integers.

 

My code so far is

 

 

 

public class mode
{
     public static void main(String args[])
     {
         int[] x;
         x = new int[10];
         System.out.print("Data Array: ");
        for(int count = 0; count<x.length; count++)
          {
        x[count] = (int) (Math.random()*10);
          System.out.print(findMode(x));
        }
    }
    public static int findMode(int[] array){

  

    }
}
 
 
 
I'm not quite sure how to create a new method, or how to find mode.

 

 

When I ran it,it seems to be missing a return statement?

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

When I ran it,it seems to be missing a return statement?

Yes, you're going to need to write the findMode function, and end it with a return line that returns the max element.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

When I ran it,it seems to be missing a return statement?

 

Yes because it has an int return type. So you need to return an integer inside the function for things to compile.

// The basic format of a functionmodifier returnType functionName(parameters){  }// modifier (http://stackoverflow.com/a/215505/1765721)publicprivateprotected// returnTypeintdoublevoidetcYou use void if you don't want to return something, otherwise you use the type you want to return.
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×