Question

How do we describe arrays with static methods and arrays as instance variables? (Java) thanks.

How do we describe arrays with static methods and arrays as instance variables? (Java) thanks.

Homework Answers

Answer #1

Could you please elaborate on what you mean when you say "describe arrays with static methods"?

For time being, I am assuming that you want to see how to have an array as an instance variable and then print it using static methods.

For the above assumption, please find the code below.

CODE

public class Main {

private static int[] arr = new int[10000];

private static int size = 0;

public static void add(int el) {

if (size >= 10000) {

System.out.println("Array is already full!!");

}

arr[size ++] = el;

}

public static void print() {

for (int i=0; i<size; i++) {

System.out.print(arr[i] + " ");

}

System.out.println();

}

public static void main(String[] args) {

for (int i=1; i<=100; i++) {

add(i);

}

print();

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
can you please explain how to complete all methods in java ? thanks /* Note:   Do...
can you please explain how to complete all methods in java ? thanks /* Note:   Do not add any additional methods, attributes.         Do not modify the given part of the program.         Run your program against the provided Homework2Driver.java for requirements. */ /* Hint:   This Queue implementation will always dequeue from the first element of         the array i.e, elements[0]. Therefore, remember to shift all elements         toward front of the queue after each dequeue. */ public class QueueArray<T>...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
In Java: Assume the existence of a Window class with int instance variables width, height, xPos...
In Java: Assume the existence of a Window class with int instance variables width, height, xPos and yPos. Define the method toString for the Window class. The String representation of a Window object should be of the form "A 'width'x'height' window at ('xPos','yPos')" where the values within the apostrophes are replaced by the actual values of the corresponding instance variables. For example, given a Window object whose width is 80, height is 20, xPos is 0 and yPos is 30,...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
java CLASS DESIGN GUIDELINES 1. Cohesion • [✓] A class should describe a single entity, and...
java CLASS DESIGN GUIDELINES 1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. • [✓] A single entity with many responsibilities can be broken into several classes to separate the responsibilities. 2. Consistency • [✓] Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. A popular style is to place the data declaration before the constructor...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Lab 03 This lab gives you exercises with using two dimensional arrays of Java and practices...
Lab 03 This lab gives you exercises with using two dimensional arrays of Java and practices with using methods that return arrays. What you should do is to download and complete the instructor’s program lab03.java: 1. Implement print2D(A) so that it prints out its 2D array argument A in the matrix form. 2. Implement add2Ds(A,B) so that it creates and returns a new 2D array C such that C=A+B. 3. Implement multiScalar2D(c,A) so that it creates and returns a new...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT