Results 1 to 4 of 4
  1. #1
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default User-Defined Classes

    I hope everyone had a happy Thanksgiving! For this last assignment, I have to set up two classes: The first class defines the variables--cell phones in this case--and the second class prompts the user to input the manufacturer, model number, and retail price then displays what was entered. The price has to be $50.00 or higher, and the model number has to be between 5-9 characters, or the program is supposed to prompt the user a second time. I can't for the life of me get the loops to work. Here's what I've got so far; any help would be greatly appreciated:

    The first class:
    Java Code:
    public class CellPhone {
        // Fields
        private String manufact;    // Manufacturer
        private String model;       // Model
        private double retailPrice; // Retail price
        public CellPhone() {
            manufact = "unknown";
            model = "unknown";       
        public CellPhone(String startManufact,
                String startModel,
                double startRetailPrice) {
            manufact = startManufact;
            model = startModel;
            retailPrice = startRetailPrice;
        public void setManufact(String newManufact) {
            this.manufact = newManufact;
        public void SetModelNumber(String newModel) {
            this.model = newModel;
        public void setRetailPrice(double newRetailPrice) {
            do {
                this.retailPrice = newRetailPrice;
            } while (newRetailPrice >= 50.00);
        public String getManufact() {
            return manufact;
        public String getModel() {
            return model;
        public double getRetailPrice() {
            return retailPrice;
    The second class:
    Java Code:
    import java.util.Scanner;
    public class CellPhoneTest {
        public static void main(String[] args) {
           CellPhone cell = new CellPhone();
            String cellManufact = cell.getManufact();
            String cellModel = cell.getModel();
            double cellPrice = cell.getRetailPrice();
        Scanner scan1 = new Scanner( );
        System.out.print( "Enter the manufacturer's name --> " );
        cellManufact = );
        Scanner scan2 = new Scanner( );
        System.out.print( "Enter the model number --> " );
        cellModel = );
        Scanner scan3 = new Scanner( );
        System.out.print( "Enter the retail price --> " );
        cellPrice = scan3.nextDouble( );
        System.out.println("\nHere is the data you provided:");
        System.out.println("Manufacturer: " +cellManufact);
        System.out.println("Model number: " +cellModel);
        System.out.println("Retail price: " +cellPrice);
    Last edited by Reptyle216; 12-03-2013 at 06:11 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: User-Defined Classes

    You start out with two problems. The first is that you create a cell phone instance and then immediately call the get methods. But the phone instance has not been populated yet so why bother? Second, your setRetailPrice method will not work that way. Think about it. You set the price and if it is less than 50 it loops forever because you haven't given it a new price. You need to prompt for the new price inside of a loop. So it would be something like:

    1. prompt for price.
    2. Check value of price and reprompt if necessary.
    3. Or accept price and break out of loop.

    Similar logic for the model number except you are checking for the length of the input string. You also have an extra set of braces that you don't need.

    Last edited by jim829; 12-03-2013 at 06:08 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Reptyle216 is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default Re: User-Defined Classes

    You're right. I think my biggest problem is figuring out when to call the methods from the first class into the second. As it stands, the second class is pretty much running on its own.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: User-Defined Classes

    Well right now you are using that main() as the entire program. When I make a (regular) Java application, the main() has only two functions;

    - initialize the application
    - get it running

    That tends to look like this. I'm leaving out exception handling because you probably have not studied that yet.

    Java Code:
    public class MyProgramName {
      public MyProgramName(){
        // initialize any class properties of MyProgramName
      public void setup(){
         // load configuration from a file perhaps?
      public void run(){
        // do things and stuff here, like calling other methods
      public static void main(String[] args){
        // take in optional command-line parameters here
        MyProgramName app = new MyProgramName();

    See, the main() does as little as possible; the application has specialized methods to do specific tasks. And now you can start to fill that run() method with whatever application logic you need it to have, such as the usage of the Cellphone class.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Set of user defined class
    By TelliameD in forum New To Java
    Replies: 4
    Last Post: 07-05-2012, 10:58 AM
  2. Problem with user defined class
    By moosethmucha in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 06:58 PM
  3. Create user-defined Classes & Methods
    By lala in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 03:03 PM
  4. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 02:02 AM
  5. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts