Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31064

Re: Calling methods from another class

Newsgroups comp.lang.java.programmer
Date 2015-02-06 18:25 -0800
References <0c5dd74d-7fd2-4328-8899-016ae5b5f3b5@googlegroups.com>
Message-ID <5a3eb348-3fa4-4a64-8754-869f4693b2e9@googlegroups.com> (permalink)
Subject Re: Calling methods from another class
From Steven-GP <ackerman.steven@gmail.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web