5 Replies - 1787 Views - Last Post: 15 April 2008 - 03:43 PM Rate Topic: -----

#1 mattjax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-March 08

Understanding the problem of assigning arrays

Post icon  Posted 30 March 2008 - 06:18 PM

I am looking for someone who can help me and my classmates out.
I do not want this done for me just made easier to understand

My understanding of arrays is they hold one type of many values.
this assignment particularly section 2.b is confusing me and some other classmates. and our teacher is hard to understand. can someone put this problme in easier terms maybe? in 1.a the teacher has the attributs to be used set as double and int..this is where i am conflicted on the array. how do you have one array with lenght of 3 when you have double and int type values can this be done?

any help is apprictated.





Individual Homework for Week 5:
1) Write a Java class named Loan.
a. The Loan class has three private attributes: term, rate and principal, where term is int type, rate is double type, and principal is double type.
b. The Loan class has a public constructor that takes term, rate and principal as parameters. The constructor will set the values of the three term, rate, and principal based on the values of the parameters.
c. The class has two public methods:
i. calculateMonthlyPayment
ii. displayAmortizationSchedule
2) Write a main method in a separate class. In the main method,
a. Declare an array of Loan type that has length 3;
b. Use a loop, based on user’s inputs of term, rate and principal create three Loan objects and store the three Loan objects in the array;
c. Invoke calculateMonthlyPayment method in each object to calculate monthly payment and display the results.
3) Submit your source files as

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding the problem of assigning arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding the problem of assigning arrays

Posted 30 March 2008 - 06:52 PM

Don't espect us to do your homework
Write a reasonnable prt of the code and we will be happy to debug it

Happy coding
PBL
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: Understanding the problem of assigning arrays

Posted 30 March 2008 - 07:11 PM

You are right in thinking that arrays are of the same data type. For the most part they are. But remember that they can also be of the same type and that type doesn't mean just int, string, char etc. It can mean class objects, other arrays, etc.

So you can have an array of objects. If you defined a class called Loan, you could have an array of Loan classes. They are all the same type, the reference type "Loan"

So how do you define such an array? Simple, just like you would with an integer array but with the class name...

Loan myloanarray[] = new Loan[2];



Here we are saying create an array of 3 Loan objects. Each element of the array is storing a Loan object. They are all the same type.

You will also find out that objects that share the same base classes can also be stored together as all the same base type. If I have a car class and I inherit from it the car "cadillac" and the car "ferrari" I could store both of these objects in an array of cars... Car mycars[] because both Cadillac and Ferrari are types of cars and inherit from car.

So the example....

Car mycars[] = new Cars[3];

// Since both are cars and come from the Car base class
// they can be stored in an array of Car classes. But they can only be
// treated as Cars while stored in the array. 

mycars[0] = new Ferrari();
mycars[1] = new Cadillac();

// Wrong, ball is not a type of car.
mycars[2] = new Ball();



Hopefully this makes sense now. You are right in assuming that arrays store the same type. But this type can be objects in addition to integers, doubles, chars etc

So to hit 2.b head on create your loan class, create an array of Loan objects and put the variables you have defined as Loan objects into it.

If you have trouble, show your code and let us know. We will guide you through. Enjoy!

:)
Was This Post Helpful? 0
  • +
  • -

#4 CanPeaceCollide  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-April 08

Re: Understanding the problem of assigning arrays

Posted 14 April 2008 - 06:03 PM

Car mycars[] = new Cars[3];



Wait, wouldn't it be

Cars[] myCars = new Cars[3];

to declare an array of type Cars? or is this one of those places where java lets you go both ways?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding the problem of assigning arrays

Posted 14 April 2008 - 06:09 PM

View PostCanPeaceCollide, on 14 Apr, 2008 - 06:03 PM, said:

Car mycars[] = new Cars[3];



Wait, wouldn't it be

Cars[] myCars = new Cars[3];

to declare an array of type Cars? or is this one of those places where java lets you go both ways?


Java let you use both ways.

When you have a single declaration both are quite equiavlent but I think that:

Cars[] myCars, usedCars, importedCars;

shows more that these are all Cars arrays rather than

Cars myCars[], usedCars[], importedCars[];

But it really a question of personal preference.

This post has been edited by pbl: 14 April 2008 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 mattjax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-March 08

Re: Understanding the problem of assigning arrays

Posted 15 April 2008 - 03:43 PM

Ok thanks for the explaination.. i appreciate it. we got the teacher to break down the code in class,
arrays always get me stuck.

ps. pbl i know you mean well but i did not want my homework done for me just explained better so i could work on it. thanks for the imput everybody
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1