Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help pls Posted by rickysri on 17 Sept 2006 at 12:31 PM
I am creating a class named Arrays Here I have to set a mutator that set the integer array nums(the attribute) values to the aray argument(parameter(.The mutator will store values of the array argument one at a time into the same element of nums array using a for loop.If array argument is shorter store only the element into nums and leave the remaining elements of the array nums unchanged.If the argument array is longer,store only as many elements as are present in the array nums so that outofboundarrayexception error doesn't occur.....
The one integer array attribute is created named nums and constructor has one integer parameter for the size of the array that value is used to instantiate the interger array called nums and then initializes the array and counting up one for each element....The accessor getNums returns the entire array nums


I dont see any error with this code that I wrote....but somehow the Testarrays class doesn't use the mutator to store the values of array ary1 and print it

Here is the code

import java.lang.*;
public class Arrys
{
int[] nums;
int[] args;

public Arrys(int size)//constructor
{
int[] nums=new int[size];
for (int n=0;n<nums.length;n++)
{
nums[n]=n;
}
}
public int[] getNums(int n) //accessor
{
return nums;
}
public int[] setNums(int[] temp) //mutator
{
for (int k=0;k<temp.length;k++)
{
if (nums[k]>temp[k])
{
nums[k]=temp[k];
}
else
{
nums[k]=temp.length;
}
}
return nums;
}

}//end class

TestArrys.java

public class TestArrys
{
public static void main(String[] args)
{
int[] ary1 = new int[5];
System.out.print("Length="+ary1.length+"\n");
for (int j=0;j<=40;j=j+10)
{
ary1[j/10]=j;
System.out.print(" " +ary1[j/10]);
}
int[] ary2= {3,5,9,11,15,18,22,23,30,31,35,39};
System.out.print("\n\nLength="+ary2.length+"\n");
for (int element:ary2)
{
System.out.print(" " +element);
}
System.out.print("\n");
System.out.println("\nStep 4");
Arrys[] myArrayObject=new Arrys[10];
for(int i=0;i<10;i++)
{
myArrayObject[i]=new Arrys(10);
myArrayObject[i].getNums(i);
}
for(int i=0;i<myArrayObject.length;i++)
{
if (myArrayObject[i]!=null)
{
System.out.print(myArrayObject[i].getNums(i));
}
}
myArratObject.setNums(ary1);
for (int k=0;k<=myArrayObject.length;k=k+10)
{
myArrayObject[k/10]=k;
System.out.print(" "+myArrayObject[k/10]);
}

}
}


Report
Re: Help pls Posted by r035198x on 18 Sept 2006 at 1:24 AM
This message was edited by r035198x at 2006-9-18 5:15:42

I would suggest the following for your Arrys class

class Arrys {
	int[] nums;
	public Arrys(int size) {
		int[] nums=new int[size];
		for (int n = 0;n < nums.length;n++) {
			nums[n] = n;
		}
	}
	public int[] getNums() {//if returning whole array, no need for parameter
		return nums;
	}
	public int[] setNums(int[] temp) {
		if(temp.length <= nums.length) {
			for (int k = 0;k < temp.length;k++) {
				nums[k] = temp[k];
			}
		}
		else {
			for (int k = 0;k < nums.length;k++) {
				nums[k] = temp[k];
			}
		}
		return nums;
}//end class



Report
Re: Help pls Posted by Gregry2 on 18 Sept 2006 at 2:08 AM
: I would suggest the following for your Arrys class
:
:
: class Arrys {
: int[] nums;
: public Arrys(int size) {
: int[] nums=new int[size];
: for (int n = 0;n < nums.length;n++) {
: nums[n] = n;
: }
: }
: public int[] getNums() {//if returning whole array, no need for parameter
: return nums;
: }
: public int[] setNums(int[] temp) {
: if(temp.length <= nums.length) {
: for (int k = 0;k < temp.length;k++) {
: nums[k] = temp[k];
: }
: }
: else {
: for (int k = 0;k < nums.length;k++) {
: nums[k] = temp[k];
: }
: }
: return nums;
: }//end class
:
USE CODE TAGS
{2}rIng
Report
Re: Help pls Posted by rickysri on 18 Sept 2006 at 7:30 AM
Thanks for the help...But I see the testArrys class does n't work I meant it doesn't use the mutator and accessor created in the Arrys class.... I get an error message...and cant figure it out ...If you know can you pls help me?...

Errors
TestArrys.java:41: cannot find symbol
symbol : method getNums()
location: class Arrys[]
myArrayObject.getNums();
^
TestArrys.java:57: cannot find symbol
symbol : method setNums(int[])
location: class Arrys[]
myArrayObject.setNums(ary1);
^
TestArrys.java:60: incompatible types
found : int
required: Arrys
myArrayObject[k/10]=k;
^
3 errors


public class TestArrys
{

public static void main(String[] args) // main method
{
int[] ary1 = new int[5]; // Defining an integer array "ary1" of size 5

System.out.print("Length="+ary1.length+"\n");

// For loop to initialize the array ary1 and print with values 0 to 40 in increments of 10

for (int j=0;j<=40;j=j+10)
{
ary1[j/10]=j;
System.out.print(" " +ary1[j/10]);
}

// Defining ary2 of size 12

int[] ary2= {3,5,9,11,15,18,22,23,30,31,35,39};
System.out.print("\n\nLength="+ary2.length+"\n");

for (int element:ary2)
{
System.out.print(" " +element);
}
System.out.print("\n");
System.out.println("\nStep 4");


Arrys[] myArrayObject={new Arrys(10)};
int[] myArray=myArrayObject.getNums();

for(int i=0;i<myArrayObject.length;i++)
{
Arrys[] myArray=new Arrys[myArrayObject[i].length];
System.out.print(" "+myArrayObject[i]);

}


for(int j=0;j<myArrayObject.length;j++)
{
System.out.print(myArrayObject[j]);
}


myArrayObject.setNums(ary1);
for (int k=0;k<=myArrayObject.length;k=k+10)
{
myArrayObject[k/10]=k;
System.out.print(" "+myArrayObject[k/10]);
}

}//end main
}//end class



: : I would suggest the following for your Arrys class
: :
: :
: : class Arrys {
: : int[] nums;
: : public Arrys(int size) {
: : int[] nums=new int[size];
: : for (int n = 0;n < nums.length;n++) {
: : nums[n] = n;
: : }
: : }
: : public int[] getNums() {//if returning whole array, no need for parameter
: : return nums;
: : }
: : public int[] setNums(int[] temp) {
: : if(temp.length <= nums.length) {
: : for (int k = 0;k < temp.length;k++) {
: : nums[k] = temp[k];
: : }
: : }
: : else {
: : for (int k = 0;k < nums.length;k++) {
: : nums[k] = temp[k];
: : }
: : }
: : return nums;
: : }//end class
: :
: USE CODE TAGS
: {2}rIng
:

Report
Re: Help pls Posted by r035198x on 18 Sept 2006 at 8:44 AM
This message was edited by r035198x at 2006-9-18 8:52:32

class Arrys {
	int[] nums;
	public Arrys(int size) {
		nums = new int[size];//notice this modification
		for (int n = 0;n < nums.length;n++) {
			nums[n] = n;
		}
	}
	public int[] getNums() {//if returning whole array, no need for parameter
		return nums;
	}
	public int[] setNums(int[] temp) {
		if(temp.length <= nums.length) {
			for (int k = 0;k < temp.length;k++) {
				nums[k] = temp[k];
			}
		}
		else {
			for (int k = 0;k < nums.length;k++) {
				nums[k] = temp[k];
			}
		}
		return nums;
	}

	public void printAll() {//make object with data do the work rather than move data around
		for (int element : nums) {
			System.out.print(" "+ element);
		}
		System.out.println();
	}

}//end class


public class TestArrys {
	public static void main(String[] args) {
		Arrys test = new Arrys(10);
		int[] shorter = new int[5];
		int[] longer = {3,5,9,11,15,18,22,23,30,31,35,39};

		for (int j=0;j<=40;j=j+10) {            //initialise shorter
			shorter[j/10] = j;
			System.out.print(" " +shorter[j/10]);
		}
		System.out.println();

		//Currently in test
		System.out.println("The initial values");
		test.printAll();

		// set shorter
		test.setNums(shorter);

		//Now in test
		System.out.println("After setting shorter");
		test.printAll();

		//set longer
		test.setNums(longer);

		//Now in test
		System.out.println("After setting longer");
		test.printAll();
	}
}


Try to understand what's going on here


Report
Re: Help pls Posted by rickysri on 18 Sept 2006 at 2:04 PM
The comment notice this modification -> nums=new int wont work in Java...I get an error saying

Arrys.java:4: '[' expected
nums = new int;//notice this modification
^
1 error


That is the problem which is why I had to give a size earlier in my previous reply mail....

Sriram

: This message was edited by r035198x at 2006-9-18 8:52:32

:
: class Arrys {
: 	int[] nums;
: 	public Arrys(int size) {
: 		nums = new int[size];//notice this modification
: 		for (int n = 0;n < nums.length;n++) {
: 			nums[n] = n;
: 		}
: 	}
: 	public int[] getNums() {//if returning whole array, no need for parameter
: 		return nums;
: 	}
: 	public int[] setNums(int[] temp) {
: 		if(temp.length <= nums.length) {
: 			for (int k = 0;k < temp.length;k++) {
: 				nums[k] = temp[k];
: 			}
: 		}
: 		else {
: 			for (int k = 0;k < nums.length;k++) {
: 				nums[k] = temp[k];
: 			}
: 		}
: 		return nums;
: 	}
: 
: 	public void printAll() {//make object with data do the work rather than move data around
: 		for (int element : nums) {
: 			System.out.print(" "+ element);
: 		}
: 		System.out.println();
: 	}
: 
: }//end class
: 
: 
: public class TestArrys {
: 	public static void main(String[] args) {
: 		Arrys test = new Arrys(10);
: 		int[] shorter = new int[5];
: 		int[] longer = {3,5,9,11,15,18,22,23,30,31,35,39};
: 
: 		for (int j=0;j<=40;j=j+10) {            //initialise shorter
: 			shorter[j/10] = j;
: 			System.out.print(" " +shorter[j/10]);
: 		}
: 		System.out.println();
: 
: 		//Currently in test
: 		System.out.println("The initial values");
: 		test.printAll();
: 
: 		// set shorter
: 		test.setNums(shorter);
: 
: 		//Now in test
: 		System.out.println("After setting shorter");
: 		test.printAll();
: 
: 		//set longer
: 		test.setNums(longer);
: 
: 		//Now in test
: 		System.out.println("After setting longer");
: 		test.printAll();
: 	}
: }
: 
: 

: Try to understand what's going on here
:
:
:

Report
Re: Help pls Posted by r035198x on 18 Sept 2006 at 11:06 PM
: The comment notice this modification -> nums=new int wont work in Java...I get an error saying
:
: Arrys.java:4: '[' expected
: nums = new int;//notice this modification
: ^
: 1 error
:
:
: That is the problem which is why I had to give a size earlier in my previous reply mail....
:
: Sriram

The problem is the design of this forum.
For some reason the rest of that line was cut off.
I wrote the line as
nums = new int[size];
but the rest of it was cut off.
I've actually used the stupid leftbr and rightbr tags



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.