aspose file tools*
The moose likes Beginning Java and the fly likes function-array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "function-array" Watch "function-array" New topic
Author

function-array

Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
Hi,
I have just started programming with Java. I have tried to write a program which calculates function y = sqrt( (a + x) / (a - x) ) meanings in this interval: xp <= x <= xg, hx (xp - first , xg - last, hx - step)
I have written this successfuly. But I also want to reply the calculation for all array A(n) elements, I do not understand how to do this. I need to create array A(n) and somehow go through it? But how this is possible, if at this time I enter "xp, xg, hx, a" by my own?
Here is my code:

Main:



Methods:



Function:


[ September 07, 2006: Message edited by: Bichumo ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!
A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!


[Jess in Action][AskingGoodQuestions]
Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
Sorry for that. I have changed my display name to my full name.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

So, you don't understand how to work with arrays in Java?

The Java Tutorial explains how to use arrays:
Language Basics - Arrays


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
Thanks for you answer. I appreciate your help. Hmmz I am trying to add to that program something like this:
1) I input the array elements number by hand and entering that array elements also by hand. (I have done this)
2) I want to do such a thing, that disables entering array elements which is not in the interval of p (xp) and g (xg). (Don't know how to do this)
3) I want to do the calculation of the function for all the array elements. (Don't know how to do this).

Please take a look at the code which I have added to my program.

Main:



Methods:




But this doesnt suite my needs. I am just happy with the "1)" step. The others doesn't work for me, and I don't know what to do with them. For example:


I if I enter the array element which is not in the interval p <= x <= g. It doesn't ignore it and continues operation. And also I don't know how to make to calculate the function for all the array elements which is between p<= x <= g.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Your code is a bit hard to understand because you're using variable and method names in your own native language (are you from Lithuania?), but here are some things that I hope will be useful to you.

First, look at this piece of your code.

This is a loop that goes over all the elements of the array (the index i goes from 0 to maselsk - 1). But what are you doing if the value of an element is not in the interval? You decrement the index variable i of the loop. That's not going to do anything useful. In fact, this will lock up your program in an infinite loop if an array element is not in the interval.

Suppose that A[0] is not in the interval. What will happen?

- First, i = 0. You check if (A[0] < p || A[0] > g).
- Suppose this is true (A[0] is not in the interval). Then you make i = -1.
- The loop continues. First, i is incremented, so i = 0 again.
- Then you're back in the loop and again you check if (A[0] < p || A[0] > g).
- This is still true, so you make i = -1.
- The loop continues, i = 0, etc.; you have an infinite loop.

If you want to call the function only for the elements that are inside the interval, just call the function inside the loop only for the elements that are inside the interval:

[ September 11, 2006: Message edited by: Jesper Young ]
Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
Yes, I am from Lithuania Next time I will throw out lithuanian language from my code and use english.
[ September 11, 2006: Message edited by: Donatas Latauskas ]
Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
But look, I have figured out, that in this method which you have offered for me, it does not calculate the function for the all array elements which is in interval, but it calculates the function as much times, as array have elements. For example at these lines:
System.out.println("Iveskite A masyvo elementu skaiciu:");
int maselsk = met.skaitoInt();
double A[] = new double[maselsk];
System.out.println("Iveskite masyvo elementus tarp: " + p + " " + "ir " + " " + g );
met.skaitoMasyva(A, maselsk);

If I enter maselsk - 4 (and all of the numbers are in the interval between 1 and 19, like 6 7 12 15). So it calculates function for four times, for the interval 1 to 19, but not for that array numbers (which are 6 7 12 15). But I want to calculate function just for that numbers which are in array.
[ September 11, 2006: Message edited by: Donatas Latauskas ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

What does the word "maselsk" mean? You make an array of length "maselsk". I assumed that "maselsk" means something like "length".

Suppose that you have an array with four elements, for example:

A[0] = 6;
A[1] = 7;
A[2] = 12;
A[3] = 15;

You can use a for-loop to loop over the array. Inside the loop you can do whatever you need to do with each array element in turn. I don't understand exactly what you want to do now and why my example wasn't exactly what you were looking for...
Donatas Latauskas
Greenhorn

Joined: Sep 07, 2006
Posts: 14
I have understood everything for now. Very thanks for your help, your method was good for me. And sorry, it's just the beginning :}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

No problem, have fun learning Java!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: function-array