• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# passing an object as a parameter

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Darego  Members

Posted 16 December 2012 - 12:52 PM

Just had an exam in college. I was asked to write a program to calculate the details of tickets sold at concerts. this was the required output(which i managed to get but without using the inner class which was required so i assume i lost marks for that)

[source lang="java"]Enter the performer of conecrt 1whamEnter the capacity of concert 1100Is ticket 1 seated? Y or NYEnter the performer of conecrt 2BAMEnter the capacity of concert 2200Is ticket 1 seated? Y or NYIs ticket 2 seated? Y or NYIs ticket 3 seated? Y or NYEnter the performer of conecrt 3BrosEnter the capacity of concert 3300Is ticket 1 seated? Y or NYIs ticket 2 seated? Y or NYIs ticket 3 seated? Y or NNConcert 1 performed by: wham€100.0, seated ticketThe total sales for this concert is: 100Concert 2 performed by: BAM€100.0, seated ticket€100.0, seated ticket€100.0, seated ticketThe total sales for this concert is: 300Concert 3 performed by: Bros€100.0, seated ticket€100.0, seated ticket€80.0, standing ticketThe total sales for this concert is: 280The highest sales of all 3 concerts is: BAM at 300[/source]
so one part of the exam which i didn't do fully was: "Create a fillList() method that takes a reference variable of type Ticket as a parameter and adds it to the list" how would i go about passing an object of an inner class as a paramitar to a method in the outer class? i managed to manipulate the question and get the required output by not even using the inner class

here is my code

Concert class with inner class Ticket:
[source lang="java"]import java.util.ArrayList;import java.util.Random;import java.util.Scanner;class Concert { Scanner input = new Scanner(System.in); Random generator = new Random(); private String performer; private int cap; private int numTickets; private int totalSales; ArrayList< String > tickets = new ArrayList< String > (); public Concert(String performer, int cap) { this.performer = performer; this.cap = cap; } public void fillList() { numTickets = 1+generator.nextInt(3); for(int i=0;i<numTickets;i++) { System.out.println("Is ticket "+(i+1)+" seated? Y or N"); String seated1 = input.next(); char seated = seated1.charAt(0); if(seated == 'Y') { String ifseated = "€100.0, seated ticket"; this.tickets.add(ifseated); totalSales = totalSales +100; } else { String ifnotseated = "€80.0, standing ticket"; this.tickets.add(ifnotseated); totalSales = totalSales +80; } } StaticFields.caclBiggestConcert(totalSales, performer); } public void displayList() { if(StaticFields.conNumber == 0) { System.out.println("Concert "+1+" performed by: "+performer); } else if(StaticFields.conNumber == 1) { System.out.println("Concert "+2+" performed by: "+performer); } else { System.out.println("Concert "+3+" performed by: "+performer); } for(int i =0; i < tickets.size();i++) { System.out.print(tickets.get(i)+"\n"); } StaticFields.conNumber++; } public void showTotalSales() { System.out.println("The total sales for this concert is: "+totalSales); } public class Ticket { private double price; private boolean seated; public Ticket(double price, boolean seated) { this.seated = seated; if(seated == true) { this.price = 100; } else { this.price = 80; } } }}[/source]
Test class:
[source lang="java"]import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); Concert con[] = new Concert[3]; for(int i =0;i < con.length;i++) { System.out.println("Enter the performer of conecrt "+(i+1)); String name = input.next(); System.out.println("Enter the capacity of concert "+(i+1)); int cap = input.nextInt(); con[i] = new Concert(name, cap); con[i].fillList(); } for(int i =0;i < con.length;i++) { con[i].displayList(); con[i].showTotalSales(); } System.out.println("The highest sales of all 3 concerts is: "+StaticFields.highestConcert+" at "+StaticFields.highestSales); }}[/source]

also had a staticfield class to handle static variables and methods

cheers guys,
D

Edited by Darego, 16 December 2012 - 01:05 PM.

### #2alnite  Members

Posted 16 December 2012 - 01:58 PM

Exams are a bitch, as it requires you to read your professor's mind.

Create a fillList() method that takes a reference variable of type Ticket as a parameter

void fillList(Ticket ticket) {
// insert ticket to your list
}


In Java, everything is passed by references except primitives (int, bool, long, float, double).

how would i go about passing an object of an inner class as a paramitar to a method in the outer class?

You can invoke a method in the outer class from the inner class.

class OuterClass {
public void someMethod(InnerClass ic) {
}

class InnerClass {
public void doStuff() {
someMethod(this);  // OK
}
}
}


Edited by alnite, 16 December 2012 - 01:59 PM.

### #3Darego  Members

Posted 16 December 2012 - 02:56 PM

thanks a lot mate. ++ need to brush up on my inner classes

Edited by Darego, 16 December 2012 - 02:56 PM.

### #4Darego  Members

Posted 16 December 2012 - 02:59 PM

so when i'm calling the fillList() method in the test class what would i use as parameters? the parameters used in the Ticket constructor? kinda confused

Edited by Darego, 16 December 2012 - 04:00 PM.

### #5TheChubu  Members

Posted 16 December 2012 - 04:11 PM

I don't quite understand the question.

fillList adds a single ticket you pass to it?

What it would be the outer class and the inner class? Ticket and concert? Ticket doesn't needs to pass to concert anything (i think).

Anyway, you have two options. Either you create your ticket object somewhere with the correct data, and pass it to fillList so it can add it.

getDataAddTicket ()
{
Ticket tmpTicket = new Ticket(data);
this.fillList(tmpTicket);
}

fillList (Ticket tmpTicket) //There you're passing a reference to a Ticket.
{
}

Or fillList is called without parameters, but you fetch the data inside that method and create the ticket right there.
fillList ()
{
get all data.
Ticket tmpTicket = new Ticket(data);
}

so when i'm calling the fillList() method in the test class what would i use as parameters? the parameters used in the Ticket constructor? kinda confused

That depends. If you don't have the ticket data at that time, you'd need to create a ticket with the default constructor and set its data up inside fillList. If you do have the data, you could use the appropiate ticket's constructor.
From test:
//If you don't have the data at that time you pass a new ticket with con[index] as the enclosing object.
//Then you ask for the data inside fillList method.
con[index].fillList(con[index].new Ticket());

//If you have the data at that time and you can provide it to ticket's constructor,
//that way fillList only has to add the ticket to the list
con[index].fillList(con[index].new Ticket(tmpDouble, tmpBool));

There! I hope I got it right

Edited by TheChubu, 16 December 2012 - 04:36 PM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #6Darego  Members

Posted 16 December 2012 - 04:38 PM

thanks a lot mate!! that explains everything

Edited by Darego, 16 December 2012 - 04:41 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.