Sign in to follow this  
Darego

passing an object as a parameter

Recommended Posts

Darego    156
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 1
wham
Enter the capacity of concert 1
100
Is ticket 1 seated? Y or N
Y
Enter the performer of conecrt 2
BAM
Enter the capacity of concert 2
200
Is ticket 1 seated? Y or N
Y
Is ticket 2 seated? Y or N
Y
Is ticket 3 seated? Y or N
Y
Enter the performer of conecrt 3
Bros
Enter the capacity of concert 3
300
Is ticket 1 seated? Y or N
Y
Is ticket 2 seated? Y or N
Y
Is ticket 3 seated? Y or N
N
Concert 1 performed by: wham
€100.0, seated ticket
The total sales for this concert is: 100
Concert 2 performed by: BAM
€100.0, seated ticket
€100.0, seated ticket
€100.0, seated ticket
The total sales for this concert is: 300
Concert 3 performed by: Bros
€100.0, seated ticket
€100.0, seated ticket
€80.0, standing ticket
The total sales for this concert is: 280
The 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 [b]takes a reference variable of type Ticket as a parameter[/b] and adds it to the list" how would i go about [i]passing an object of an inner class as a paramitar to a method in the outer class?[/i] 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

Share this post


Link to post
Share on other sites
alnite    3436
Exams are a bitch, as it requires you to read your professor's mind.

[quote name='Darego' timestamp='1355683969' post='5011322']
Create a fillList() method that takes a reference variable of type Ticket as a parameter
[/quote]

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

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

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

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

[CODE]
class OuterClass {
public void someMethod(InnerClass ic) {
}

class InnerClass {
public void doStuff() {
someMethod(this); // OK
}
}
}
[/CODE] Edited by alnite

Share this post


Link to post
Share on other sites
Darego    156
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

Share this post


Link to post
Share on other sites
TheChubu    9452
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.

[CODE]getDataAddTicket ()
{
ask the user the data.
Ticket tmpTicket = new Ticket(data);
this.fillList(tmpTicket);
}

fillList (Ticket tmpTicket) //There you're passing a reference to a Ticket.
{
this.tickets.add(tmpTicket);
}[/CODE]

Or fillList is called without parameters, but you fetch the data inside that method and create the ticket right there.
[CODE]
fillList ()
{
get all data.
Ticket tmpTicket = new Ticket(data);
this.tickets.add(tmpTicket);
}[/CODE]
[quote name='Darego' timestamp='1355691599' post='5011352']
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
[/quote]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:
[CODE]
//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
//without asking for more information.
con[index].fillList(con[index].new Ticket(tmpDouble, tmpBool));
[/CODE] There! I hope I got it right :P Edited by TheChubu

Share this post


Link to post
Share on other sites
Darego    156
thanks a lot mate!! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] that explains everything Edited by Darego

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this