Write a method called setTo5 which is passed a reference to an array of ints and sets the contents of that array to all 5s. This is what I have so far, and it's giving me an error on the second line after public static void main(...);. How to make a better setter?
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
roop.setTo5(5);
}
public void setTo5(int poop){
for(int i = 0; i<n ; i++){
poop = boop[i];
}
}
}
Try something like this:
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
//Create instance of Set5 class to call setter
Set5 set5reference = new Set5();
set5reference.setTo5(roop);
//All the values in roop will now be 5 as set by setter.
}
//change this method to accept array reference like this
public void setTo5(int[] poop){
for(int i = 0; i<n ; i++){
poop[i] = 5;
}
}
}
To fill an array entirely with some value use:
java.util.Arrays.fill(poop, 5)
In your case:
public class Set5 {
private static int n = 8;
//static int[] boop = new int[n]; // unused variable
public static void main(String[] args){
int[] roop = new int[n];
setTo5(roop);
print(roop);
}
public static void setTo5(int[] poop){
java.util.Arrays.fill(poop, 5)
// for(int i = 0; i<poop.length ; i++){
// poop[i]=5;
//}
}
public static void print(int[] poop){
for(int i = 0; i<poop.length ; i++){
System.out.println("array["+i+"] = "+poop[i]);
}
}
}
Related
i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}
The problem is in method "calculusRank". If I delete that method and related parts to it, aplication will work, if I create a different notepad with same code(method and main code related to that method) it's work.
Also I wanna say I'm a beginner and this is my first "relative" large application.
This is the code with problem that doesn't compile:
import java.util.Random;
import java.lang.Math;
public class MultilevelSystem1 {
static String[][] createMembers (int a) {
Random nr = new Random ();
String[][] membersName = new String [a][2];
for(int j=0; j<2;j++)
for(int i=0; i<a; i++) {
if(j==0||i==0) membersName[i][j]="Nume"+(i+1);
else membersName[i][j]="Nume"+(nr.nextInt(i)+1);
}
return membersName;
}
static String[][] createIncomes (String[][] a,int b){
Random nr = new Random ();
String[][] incomes = new String [b][2];
int j=0;
for(int i=0;i<a.length && j<b;i++)
if(nr.nextInt(2)==1){ incomes[j][0]= a[i][0];
incomes[j][1]=Integer.toString((nr.nextInt(10)+1)*50);
j++;}
return incomes;
}
static class Members extends MultilevelSystem1 {
double capital;
String name;
int rank;
String superior;
int ID;
int calculateRank (int r, Members[] membersArray) {
if(this.superior!=null)
for(int i=0;i<membersArray.length;i++)
if((this.superior).equals(membersArray[i].name)){r=membersArray[i].calculateRank(r,membersArray);
r++;
break;}
return r;
}
static int[] calculate (int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
}
public static void main(String[] args) {
final int n = 30;
final int m = 10;
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
String[][] entryDataMembers = createMembers(n);
String[][] entryDataIncomes = createIncomes(entryDataMembers,m);
Members[] membersArray = new Members[n];
for (int i=0; i<n; i++) {
membersArray[i]=new Members();
membersArray[i].name = entryDataMembers[i][0];
if(i!=0) membersArray[i].superior= entryDataMembers[i][1];
else membersArray[0].superior= null;
}
for (int i=0; i<n; i++)
membersArray[i].rank = membersArray[i].calculateRank(1,membersArray);
}
}
And this work:
public class test {
static int[] calculate(int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
public static void main (String[]args) {
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}
int[] c = Members.calculate(a,b);
In you code there is compilation issue for calculate(a,b) method, this method present inside Members class. calculate(a,b) is static method so you can call this method using Members class.
The method which compiler couldn't find is "calculate".
Use
int[] c= Members.calculate(a,b);
to access static method of member class. (Around 62 line of code inside main method)
I am trying to add random numbers to an empty array 20 numbers 0-99. When I run the code below it prints out 51 numbers and they are all 0.
Can someone please help me figure out what I am doing wrong here.
import java.util.Random;
public class SortedArray
{
int randomValues;
int[] value;
public SortedArray()
{
}
public int getRandom()
{
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues = random.nextInt(100);
}
return randomValues;
}
public int getArray()
{
int result = 0;
value = new int[randomValues];
for(int item : value)
{
System.out.println("The array contains " + item);
}
return result;
}
}
Here is my main method
public class ReturnSortedArray
{
public static void main(String[] args)
{
SortedArray newArray = new SortedArray();
int random = newArray.getRandom();
int array = newArray.getArray();
System.out.println(array);
}
}
In your method getArray
the code
value = new int[randomValues];
is simply creating a new empty int array of size ramdomValues.
As the default value of an int is 0, that is what you are getting
Also in your method getRandom you are setting the same value time and time again
for (...)
randomValues = random.nextInt(100);
try
public int[] getRandomArr()
{
int randomValues [] = new int [20];
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues[j] = random.nextInt(100);
}
return randomValues;
}
I see a few issues, you should probably set the values in your constructor. You could also call it a set method (since it's not actually a get). Also, your getArray() doesn't return an array. So, I think you really wanted something like this,
public class SortedArray {
private Random random = new Random();
private int[] value = new int[20];
public SortedArray() {
super();
setRandomValues();
}
public void setRandomValues() {
for (int j = 0; j < value.length; j++) {
value[j] = random.nextInt(100);
}
}
public int[] getArray() {
return value;
}
}
And then your main method, should be updated like
public static void main(String[] args) {
SortedArray newArray = new SortedArray();
int[] array = newArray.getArray();
System.out.println(Arrays.toString(array));
}
I'm trying to fill an array using a method and later print that array out.
However when I try to do so all it gives me are zeroes. I think my fill method is not working properly but I'm not sure why. I'm trying to understand arrays but so far no good. I would prefer an explanation rather than an answer. If I can get this myself it would be best.
import java.util.Scanner;
public class diverScore {
static double score = 0;
static double validDegreeOfDiff = 0;
public static void main(String[] args) {
double[] score = new double[6];
inputAllScores(score);
printArray(score);
}
public static double[] inputAllScores(double[] x) {
Scanner s = new Scanner(System.in);
double[] array_score = new double[6];
for (int i = 0; i < 6; i++) {
System.out.println("What is the score given by the judge?");
array_score[i] = s.nextDouble();
}
return array_score;
}
public static void printArray(double[] j) {
for (int i = 0; i < 6; i++) {
System.out.println("The array is:" + j[i]);
}
}
}
In your inputAllScores, you're writing to a new local array, and returning it, but you're not using the returned array. It would be better if you wrote to the array that you passed into that method (which inside the method is called x).
try
import java.util.Scanner;
public class DiverScore {
static double score = 0;
static double validDegreeOfDiff = 0;
public static void main(String[] args) {
// double[] score = new double[6];
double[] score = inputAllScores(/*score*/);
printArray(score);
}
public static double[] inputAllScores(/*double[] x*/) {
Scanner s = new Scanner(System.in);
double[] array_score = new double[6];
for (int i = 0; i < 6; i++) {
System.out.println("What is the score given by the judge?");
array_score[i] = s.nextDouble();
}
return array_score;
}
public static void printArray(double[] j) {
for (int i = 0; i < 6; i++) {
System.out.println("The array is:" + j[i]);
}
}
}
double[] score = new double[6];
This line simply initializes an array of type double with 6 indexes allocated for it, with each resulting in 0 when printed out.
You could simply change the code in main to this, thus actually using the return value of the inputAllScores function.
public static void main(String[] args) {
double[] score = new double[6];
printArray(inputAllScores(score));
}
HTH
This is my program that makes some calculations to a numbers into the array named "initialMarks". However I would like to fill the initialMarks array from another class using scanner.Could you help me to figure it out how to do that? Is it possible to outprint the result array "result" in a third class?
public class testingN
{
public static void main(String[] args)
{
int [] initialMarks = new int [4];
int [] result = new int [6];
result[0] = computedMarks(initialMarks[0], initialMarks[1])[0];
result[1] = computedMarks(initialMarks[2], initialMarks[3])[1];
for(int i=0; i< result.length; i++)
System.out.println(result[i]);
}
public static int [] computedMarks(int mark1, int mark2)
{
int [] i= new int [6];
for (int j = 0; j < i.length; j++)
{
if ((mark1 < 35 && mark2 > 35) || (mark1 > 35 && mark2 < 35))
{
i[j] = 35;
}
else
{
i[j] = (mark1 * mark2);
}
}
return i;
}
}
Your other class can have a method that returns a stream, and you can feed that into your Scanner's constructor.
In your other class's String getInitialMarks() method:
// Generate a string with all the "marks" as "stringWithMarks", separated by "\n" characters
InputStream is = new ByteArrayInputStream( stringWithMarks.getBytes( "UTF-8" ) );
return is;
Then in your first class, otherClass:
Scanner scanner = new Scanner(otherClass.getInitialMarks());
And proceed to read in the marks as if it were user input.
public class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
public static void main(String[] args)
{
Declaring result as class member (global variable) and being static should help your cause
Example: using from another class
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}
Edit: Run the code below Here. You'll see it works just fine.
class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
}
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}