Help pls - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help pls

rickysririckysri Posts: 21Member
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;ntemp[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+"
");
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("

Length="+ary2.length+"
");
for (int element:ary2)
{
System.out.print(" " +element);
}
System.out.print("
");
System.out.println("
Step 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]);
}

}
}


Comments

  • r035198xr035198x Posts: 5Member
    [b][red]This message was edited by r035198x at 2006-9-18 5:15:42[/red][/b][hr]
    I would suggest the following for your Arrys class

    [code]
    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

    [/code]

  • Gregry2Gregry2 Posts: 607Member
    : 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
    :
    [size=5][blue]USE CODE TAGS[/blue][/size]
    {2}rIng
  • rickysririckysri Posts: 21Member
    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+"
    ");

    // 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("

    Length="+ary2.length+"
    ");

    for (int element:ary2)
    {
    System.out.print(" " +element);
    }
    System.out.print("
    ");
    System.out.println("
    Step 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
    : :
    : [size=5][blue]USE CODE TAGS[/blue][/size]
    : {2}rIng
    :

  • r035198xr035198x Posts: 5Member
    [b][red]This message was edited by r035198x at 2006-9-18 8:52:32[/red][/b][hr]
    [code]
    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();
    }
    }

    [/code]
    Try to understand what's going on here


  • rickysririckysri Posts: 21Member
    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

    : [b][red]This message was edited by r035198x at 2006-9-18 8:52:32[/red][/b][hr]
    : [code]
    : 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();
    : }
    : }
    :
    : [/code]
    : Try to understand what's going on here
    :
    :
    :

  • r035198xr035198x Posts: 5Member
    : 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[leftbr]size[rightbr];
    but the rest of it was cut off.
    I've actually used the stupid leftbr and rightbr tags
Sign In or Register to comment.