Re: Calling methods from another class
Hey, thank you that's an awesome idea. Thank you.
-Steve
On Wednesday, February 4, 2015 at 10:57:08 AM UTC-8, Steven-GP wrote:
Greetings List,
Just joined this forum. Just using Java as a student. Having problems wit=
h some code I wrote. Here is the code. Not sure if this is the way to post =
to this forum. Correct me if I'm wrong, and I'll edit my post appropriately=
..
<---------------------------Code is Below--------------------------------=
-->
I have a very standard Lab assignment. It's probably been seen a lot. I w=
rote the first part not realizing I had to write a second class to do use t=
he methods. I'm not sure how to change my program to call methods from my s=
econd class instead of doing all my calculations with user input in my firs=
t class.
Here's the first class' code:
[highlight=Java]
package tickets;
/**
*
* @author steven.
*/
//Imports classes used for "Ticket" application.
import java.util.*;
import java.text.DecimalFormat;
public class Tickets {
/**
* This is the base of my application.
*/
public static void main(String[] args) {
// Create a scanner object to get input from user.
Scanner input = new Scanner(System.in);
//Declares variables for seating.
int a, b, c;
//Declares doubles to store user input and store sales total in.
double A, B, C, salesTotal;
//User input for amount sold and price of "A" tickets.
System.out.print("Enter number of A tickets sold, please.");
a = input.nextInt();
System.out.print("Enter price of A tickets, please.");
A = input.nextDouble();
//User input for amount sold and price of "B" tickets.
System.out.print("Enter number of B tickets sold, please.");
b = input.nextInt();
System.out.print("Enter price of B tickets sold, please");
B = input.nextDouble();
//User input for amount sold and price of "C" tickets.
System.out.print("Enter number of C tickets sold, please.");
c = input.nextInt();
System.out.print("Enter price of C tickets, please");
C = input.nextDouble();
//This calculates the total sales for all tickets sold.
salesTotal = ((a*A)+(b*B)+(c*C));
//Displays Total Sales and seats.
System.out.println("Number of A tickets sold: "+ a);
System.out.println("Number of B tickets sold: "+ b);
System.out.println("Number of C tickets sold: "+ c);
System.out.println("Total ticket sales: " + salesTotal);
//
}
}
[/highlight]
That code works fine, compiles without errors and correctly calculates an=
d displays everything correctly. I didn't realize I needed to implement a s=
econd class in this program. Here is my attempt:
[highlight=Java]
package tickets;
/**
*
* @author Steven
*/
public class Seats {
//Declare data members.
private int seats;
//Constructors.
public Seats()// My default constructor.
{
seats = 0;
}
public Seats(int numSeats)
{
setSeats(numSeats);
}
//instance methods
public void setSeats(int numSeats)
{
//validate parameter
seats = numSeats;
}
public int getSeats()
{
return seats;
}
public int computeSeats()
{
return seats + seats + seats;
}
public void setPrice(double priceSeats)
{
private double price;
}
}
[/highlight]
The code is obviously incomplete. I have not tried to compile, nor would =
I expect it to compile right. I'm not sure how to move my calculations from=
the first class shown above into my second class and use them as methods. =
Can someone please help? Am I moving in the right direction?
Thanks a lot. I appreciate someone that knows what they're doing to tell =
me what I need to do.
-Steven