Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Make a constructor

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Make a constructor

    Hey guys, for this code I need to have a proper constructor and I dont know which part of the code to make as a constructor. I thought the prices and the size of the pizza I can make as a constructor but I dont know how to do it.

    Here is my code:

    import java.util.;
    public class Pizza {
    public static void main(String[] args)
    {
    final int NUMBER_CHOICES = 4;
    String[] Size = {"S", "M", "L", "X"};
    double[] size_prices = {6.99, 8.99, 12.50, 15.00};
    String chosenSize;
    double pizzaPrice = 0.00;
    boolean validChoice = false;
    Scanner inputDevice = new Scanner(System.in);
    System.out.print("Choose your pizza sizes - S, M, L, or X: ");
    chosenSize = inputDevice.nextLine();
    for(int s = 0; s < NUMBER_CHOICES; ++s)
    {
    if(chosenSize == null ? Size[s] == null : chosenSize.equals(Size[s]));
    {
    validChoice = true;
    pizzaPrice = size_prices[s];
    }
    }
    if(validChoice)
    System.out.println("The size f your pizza is " +
    chosenSize + " and total need to pay is $" + pizzaPrice);
    else
    System.out.println("Sorry - Wrong size! Please try again");
    }
    }

  2. #2
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Make a constructor

    split the code into sections

    public class Pizza {
        some common variables
     
        Pizza() {    }
     
        Pizza(String size) { }
     
        double sizeToPrice(String size) { }
     
        void sizeInput() {   }
     
        public static void main(String[] args) {
           Pizza p = new Pizza();       
        }
    }
    Last edited by zemiak; April 5th, 2021 at 06:24 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Make a constructor

    Can you try in a complete code? Cause I am so confused how to put it all together

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Make a constructor

    Take a look at the tutorial: https://docs.oracle.com/javase/tutor...structors.html
    Make a try. If there are error messages, copy the full text and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What difference does an additional constructor make to your program?
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 18th, 2019, 07:23 AM
  2. Replies: 3
    Last Post: March 4th, 2013, 10:51 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM
  5. Design of class named Fan to represent a Fan
    By qaromi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2009, 02:28 PM

Tags for this Thread