• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Darego

passing an object as a parameter

5 posts in this topic

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
0

Share this post


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

Share this post


Link to post
Share on other sites
thanks a lot mate. ++ need to brush up on my inner classes Edited by Darego
0

Share this post


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

Share this post


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

Share this post


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

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  
Followers 0