I want to call this method:
public int ArraySum(int[] a)
{
int sum = 0;
int Element;
for(Element = 0; Element < a.length; Element++)
{
sum = sum + a[Element];
}
return sum;
}
in this method (which is in a different class):
public int Mean()
{
return (something.ArraySum(a))/2;
}
I know that I probably need to create an object but I'm not sure exactly how.
Just an example:
public class C1
{
//all the fields and stuff
public int hello(int a)
{
//all the code
}
public static int hey(int a)
{
//all code
}
}
Note: One of the functions is static. Observe how we call them.
public class C2
{
//all fields and stuff
public void callerFunction()
{
C1 obj=new C1();
//created an object of class C1
obj.hello(5);
C1.hey(10);
//only class name is required while calling static methods.
}
}
You need to create an object of the class ArraySum method is present in. E.g. if it's present in Calculator class like below:
public class Calculator{
public int ArraySum(int[] a){
int sum = 0;
int Element;
for(Element = 0; Element < a.length; Element++)
{
sum = sum + a[Element];
}
return sum;
}
}
Then, what you need to do is (assuming that class doesn't define any non zero argument constructor),
Calculator calculator = new Calculator();
calculator.ArraySum(..);
Related
So I have this code in the main class
public class OneDArrays
{
public static int[] create (int size)
{
int[] a1 = new int[size];
for (int i = 0; i < size; i++)
{
a1[i] = i*2+1;
}
return a1;
}
public int sumSome (int[] b1, int howmany)
{
int sum = 0;
if (howmany <= b1.length)
{
for (int i = 0; i < howmany; i++)
{
sum = sum + b1[i];
}
}
else
{
sum = -1;
}
return sum;
}
public int[] grow (int[] c1, int extra)
{
int[] newArray = new int[c1.length+extra];
for (int i = 0; i < newArray.length; i++)
{
while (i <= c1.length)
{
newArray[i] = c1[i];
i++;
}
newArray[i] = 0;
}
return newArray;
}
public void print (int[] d1)
{
for (int i = 0; i < d1.length; i++)
{
System.out.println (d1[i] + ", ");
}
}
}
And then I have my tester class,
public class OneDArraysTester
{
public static void main (String[] args)
{
int[] test1;
test1.create (5);
}
}
How do retrieve the method from the first class? I get the error that "create" is an undeclared method. If the "create" method were a constructer, I know I could just type create test1 = new create (5) but I don't see a way to turn it in to a constructer, so what's the way of doing that but for a method?
You invoke a static method with the classname. Literally className.methodName. Like,
int[] test1 = OneDArrays.create(5);
You have made a class named OneDArrays so you can call it's methods by creating an instance or object of that class.
like this :
OneDArrays ObjectOfClass = new OneDArrays();
int test1[] = ObjectOfClass.create(5);
similarly you can also call other methods of that class by accessing methods of this newly created object ObjectOfClass.
like :
sumOfArray = ObjectOfClass.sumSome(test1,3);
int biggerTest1[] = ObjectOfClass.grow(test1,10);
If you want to make create method works as a constructor than you can but you cannot return value from a constructor so you cannot return your array from that constructor.
Since you have declared the create method as static, #ElliotFrisch is the best way. But, it is not always a good idea to make methods static. So another way to achieve what you want would be to make the create method non-static.
public int[] create (int size){/*Method Body*/};
And then create an object of the OneDArray class to access the method.
OneDArrays oneDArrays = new OneDArrays();
int[] test1 = oneDArrays.create(5);
or,
int[] test1 = new OneDArrays().create(5);
I'm trying to write a Linear List based on arrays, but make the list be able to store any value by using Java Generics. This way I can create other programs that utilize it, but pass in different data types. I'm not entirely sure how to do this, any help would be appreciated.
I guess Im struggling trying to set it up and create the functions. The generic type really messes me up.
For example, trying to add a removeFirst() function, I cant use a loop like this:
for (int i = 0; i < n - 1; i++)
newList[i] = newList[i + 1];
— as it says The type of the expression must be an array type but it resolved to ArrayList.
Fair warning, I'm still learning data structures. This is what I have so far:
import java.util.ArrayList;
public class LinearList<T> {
private static int SIZE = 10;
private int n = 0;
private final ArrayList<T> newList = new ArrayList<T>(SIZE);
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
public void add(T value, int position) {
newList.add(position, value);
n++;
}
public void addFirst(T value) {
newList.add(0, value);
n++;
}
public void removeLast() {
T value = null;
for (int i = 0; i < newList.size(); i++)
value = newList.get(i);
newList.remove(value);
n--;
}
public void removeFirst() {
newList.remove(0);
n--;
}
public T first() {
return newList.get(0);
}
public T last() {
int value = 0;
for (int i = 0; i < newList.size() - 1; i++)
value++;
return newList.get(value);
}
public int count() {
return n;
}
public boolean isFull() {
return (n >= SIZE);
}
public boolean isEmpty() {
return (n <= 0);
}
//part 4
public void Grow() {
int grow = SIZE / 2;
SIZE = SIZE + grow;
}
public void Shrink() {
int grow = SIZE / 2;
SIZE = SIZE - grow;
}
public String toString() {
String outStr = "" + newList;
return outStr;
}
}
A good start would be to make it non-generic with a class you are comfortable with, such as an Integer.
Once you have it set up, you can then make it generic by adding <T> to the class name, then replacing all references of Integer with T.
public class MyArray{ becomes public class MyArray<T>{
public Integer add(Integer value){ becomes public T add(T value){
See What are Generics in Java? for more help
so i want to pass s to a child class that over writes this class and use s in the childs method as i wish to print a message from the child class depending on the number s is
t is a private array and nt is a private number, is it possible to do so
void c() {
int s= 0;
for(int i = 0; i < t.length; i++){
s+= t[i];
}
s= s/nt;
}
Why don't you make the method return the value?
I would do something like this:
//parent class method
int c() {
int s= 0;
for(int i = 0; i < t.length; i++){
s+= t[i];
}
s= s/nt;
return s;
}
//child class method
void printDependingOnNumber() {
if(super.c()==0 /*or whatever number*/){
//print here
}
}
To me it seems that the method "c" only calculates the value of "s", so making it a void isn't very convenient. Making it return int allows you to calculate that value in the child class too.
I am doing a school project in java and I am trying, in a method, to refer to the class.
import java.util.ArrayList;
public class NumberIndex extends ArrayList<Integer> {
private int s;
public NumberIndex (){
super();
s = 10; // would be better if it was class.size()
//but I don't know how to refer to the class
}
public NumberIndex (int x){
super(x);
s = x;
}
public void addWord(int num) {
for(i = 0; i < s; i++)
//trying to make it so that for each Integer in ArrayList,
// if there exists an Integer that has the value num, nothing would
//happen. Else creates new Integer and adds it to the List
So in order for me to finish this code, all I need is a way to reference the class object NumberIndex itself.
Since add word is a member function use this. that refers to the current NumberIndex object.
EDIT:
public class NumberIndex extends ArrayList<Integer> {
public NumberIndex() {
super(10);//setting the size of your NumberIndex object -> list size
}
public NumberIndex(int x) {
super(x);//setting the size of your NumberIndex object -> list size
}
public void addWord(int num) {
if(!this.contains(num)){//if the current NumberIndex object (list) does not contain num
this.add(num);//to the current NumberIndex object (list) add num
}
}
}
It's a requirement of my school assignment that I use "this" in the following program. However, I can't quite figure out where I could put this. I keep getting a "non-static variable this cannot be referenced from a static context" error.
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
System.out.println("Enter the upper limit for the prime numbers computation: ");
int upperLimit = new Scanner(System.in).nextInt();
int count = 0;
for(int number = 2; number<=upperLimit; number++)
{
if(isPrime(number))
{
System.out.println(number);
count++;
}
}
System.out.println("Number of primes generated: " + count);
}
public static boolean isPrime(int number)
{
for(int i=2; i<number; i++)
{
if(number%i == 0)
{
return false;
}
}
return true;
}
}
The Java keyword this refers to the instance of your class that invoked an instance method. A static method is general to its class, and so you cannot reference any instance (non-static) variables from within it. You can only access instance variables like this from within an instance method, that is, a method that is not defined as static.
So, you would need to create an instance method (of which there are none in your class), in order to use this.
This is nothing more than a reference to the object on which the method was called. Static methods on the other hand can operate without any instance of the class even exisiting, therefore they can't have reference to any object. That's why you can't use this in static method. If you really need this, you have to remove static keywords from your functions and use instance variables in those functions anyhow.
public class PrimeNumber
{
public int count = 0;
public int upperLimit;
public static void main(String args[])
{
PrimeNumber pn = new PrimeNumber();
System.out.println("Enter the upper limit for the prime numbers computation: ");
pn.upperLimit = new Scanner(System.in).nextInt();
pn.doCheck();
System.out.println("Number of primes generated: " + pn.count);
}
public void doCheck() {
for (int number = 2; number <= this.upperLimit; number++)
{
if (this.isPrime(number))
{
System.out.println(number);
count++;
}
}
}
public boolean isPrime(int number)
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}