Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31044 > unrolled thread
| Started by | Steven-GP <ackerman.steven@gmail.com> |
|---|---|
| First post | 2015-02-04 10:56 -0800 |
| Last post | 2015-02-06 18:25 -0800 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Calling methods from another class Steven-GP <ackerman.steven@gmail.com> - 2015-02-04 10:56 -0800
Re: Calling methods from another class FredK <fred.l.kleinschmidt@gmail.com> - 2015-02-04 14:42 -0800
Re: Calling methods from another class Steven-GP <ackerman.steven@gmail.com> - 2015-02-06 18:25 -0800
| From | Steven-GP <ackerman.steven@gmail.com> |
|---|---|
| Date | 2015-02-04 10:56 -0800 |
| Subject | Calling methods from another class |
| Message-ID | <0c5dd74d-7fd2-4328-8899-016ae5b5f3b5@googlegroups.com> |
Greetings List,
Just joined this forum. Just using Java as a student. Having problems with 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 wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first 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 and displays everything correctly. I didn't realize I needed to implement a second 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
[toc] | [next] | [standalone]
| From | FredK <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2015-02-04 14:42 -0800 |
| Message-ID | <3dc39ccd-9db4-415a-880b-eff95072af9b@googlegroups.com> |
| In reply to | #31044 |
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 with 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 wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first 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 and displays everything correctly. I didn't realize I needed to implement a second 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;
> }
Instead of setSeats(), you might consider a incrementSeats() method,
something like
public int incrementSeats( int numToAdd ) {
seats += numToAdd;
if ( seats < 0 ) {
// an error - you refunded more than were sold
seats = 0;
} else if ( seats > MAX_NUMBER_OF_SEATS ) {
// a possible error - you are oversold
}
}
Then in your main, when you create Seats instances,
specifying the price for each seat type:
Seats seatsA = new Seats( double priceA );
Seats seatsB = new Seats( double priceB );
Seats seatsC = new Seats( double priceC );
And when after you ask for number sold,
seatsA.incrementSeats( numASeatsSold );
seatsB.incrementSeats( numBSeatsSold );
etc.
>
> public int computeSeats()
> {
> return seats + seats + seats;
> }
>
> public void setPrice(double priceSeats)
> {
> private double price;
'price' should not be declared here; it should be an instance variable
just like 'seats' is.
>
> }
>
> }
> [/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?
>
--
Fred K
[toc] | [prev] | [next] | [standalone]
| From | Steven-GP <ackerman.steven@gmail.com> |
|---|---|
| Date | 2015-02-06 18:25 -0800 |
| Message-ID | <5a3eb348-3fa4-4a64-8754-869f4693b2e9@googlegroups.com> |
| In reply to | #31044 |
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 with 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 wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first 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 and displays everything correctly. I didn't realize I needed to implement a second 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web