The Artima Developer Community
Sponsored Link

Java Answers Forum
hey i understand the array concept but struggling

3 replies on 1 page. Most recent reply: Nov 17, 2002 11:24 AM by Cshah

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Cshah

Posts: 16
Nickname: tkc204
Registered: Oct, 2002

hey i understand the array concept but struggling Posted: Nov 14, 2002 5:44 PM
Reply to this message Reply
Advertisement
/*This assignment will give you practice using arrays.
Create a class "IntVector" that contains a 1-dimensional array of integers.
The class should include the following:

An instance variable that holds the integers.
This will have to be an array variable.
A constructor that takes an array of integer as a parameter,
allowing you to set the contents of the object to an initial value.
A "display" method which prints out the contents of the object.
A "min" method which returns the minimum value in the object.
A "mean" method which returns the average of all the integers stored in the object.
A "sum" method which takes another IntVector as a parameter, and returns a new
IntVector which is the element-wise sum of the two original objects. For example,
if an IntVector object contains the integers { 4, 10, 7, 2 }, and you call
"sum" passing as an argument another IntVector which contains { 1, 2, 3, 4 },
the result should be an IntVector which contains { 5, 12, 10, 6 }.
This method will have to check to make sure the arrays in the invoking object
and the parameter have the same length.
(Hint: the "sum" method is the hardest part, so do this last.)
Write a "main" method that tests each of the methods described above and
demonstrates that they all work correctly. Remember to test the case where
the length is zero.
*/

public class IntVector
{
public static void main(String[] args)
{
int[] numbers = { 4, 10, 7, 2 };
display( numbers );




}
public static void display(int[] numbers)
{
int i;
for( i = 0; i < numbers.length; i++ )
{
System.out.println( numbers[ i ] );
}
System.out.println( mean(numbers) );

}
public static void min(int[] numbers)
{
for(int i = 0; i < numbers.length; i++ )
{
if ( )
{

}
}


}
public static void mean(int[] num)
{
int sum=0;
int mean;
for(int i = 0; i < num.length; i++)
{
sum += num;


}
mean = sum/num.length;

}
public static void sum()
{

}


}


Dharmendra

Posts: 10
Nickname: dh547
Registered: Oct, 2002

Re: hey i understand the array concept but struggling Posted: Nov 14, 2002 9:25 PM
Reply to this message Reply
public class IntVector
{
int[] arr;

public IntVector(int[] ar){
this.arr = arr;
}
public static void main(String[] args)
{
int[] numbers = { 4, 10, 7, 2 };
display( numbers );




}
public static void display(int[] numbers)
{
int i;
for( i = 0; i < numbers.length; i++ )
{
System.out.println( numbers[ i ] );
}
System.out.println( mean(numbers) );

}
public static void min(int[] numbers)
{
int min = 0;
for(int i = 0; i < numbers.length; i++ )
{
if (min > numbers )
{
min = numbers;
}
}


}
public static void mean(int[] num)
{
int sum=0;
int mean;
for(int i = 0; i < num.length; i++)
{
sum += num;


}
mean = sum/num.length;

}
public static void sum(int[] num)
{
if(num.length == arr.length)
System.out.println("sum of arrays = "+arr+num);
}


}

Dharmendra

Posts: 10
Nickname: dh547
Registered: Oct, 2002

Re: hey i understand the array concept but struggling Posted: Nov 14, 2002 9:27 PM
Reply to this message Reply
I didn't test as i hate testing

small error

if(min > numbers)
min = numberes;

Cshah

Posts: 16
Nickname: tkc204
Registered: Oct, 2002

Re: hey i understand the array concept but struggling Posted: Nov 17, 2002 11:24 AM
Reply to this message Reply
thanks alot for the help

Flat View: This topic has 3 replies on 1 page
Topic: Show picture stored in database Previous Topic   Next Topic Topic: adding the text file to a database

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use