passing an object as a parameter

Started by
4 comments, last by Darego 11 years, 4 months ago
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 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 = new Concert(name, cap);
con.fillList();
}
for(int i =0;i < con.length;i++)
{
con.displayList();
con.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
Advertisement
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?
[/quote]

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
}
}
}
thanks a lot mate. ++ need to brush up on my inner classes
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
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 ()
{
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);
}


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);
this.tickets.add(tmpTicket);
}


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
//without asking for more information.
con[index].fillList(con[index].new Ticket(tmpDouble, tmpBool));
There! I hope I got it right :P

"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

thanks a lot mate!! tongue.png that explains everything

This topic is closed to new replies.

Advertisement