CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2
  • Learn How to Create Dual Mode Windows Services

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 6th, 2009, 06:16 PM
    mojoopop mojoopop is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    mojoopop is an unknown quantity at this point (<10)
    Exclamation Java Pizza Program Help

    so here is the big problem... the point of this assignment was to make a program that accepts 2 arguments of which only the first letter of each is read so i understand i would have to use the .charAt method to read Them

    after it reads the strings and accepts the char values it is supposed to switch them out for strings after it has done that it must check the validity of the switch and output a message saying either ("your size is: +checksize") or ("invalid size") i understand that part

    so here is the point of my problem i dont know how to get my argument that i inputed into my checkorder class from my orderform class? both the orderform and the check order use my pizza class.

    here are my three classes so far...

    p.s. i have also included the slides where i was given instructions on what to do in case you can not understand what i said...

    Code:
    /**
     * Write a description of class Pizza here.
     *
     * @author Michael Wojtysiak
      * @version 1.1
     */
    public class Pizza
    {
       private String size;
       private String toppings;
       private static int PizzasSold;
     
      
       public  Pizza()
       {
           System.out.println("Pizza One - Default Constructor");
           setSize("Small");
           setToppings("Regular");
           System.out.println("Size: " + size);
           System.out.println("Toppings: " + toppings);
           System.out.println("");
        }
        
        public Pizza(String setSize, String setToppings)
        {
            this.size = setSize;
            this.toppings = setToppings;
            System.out.println("Pizza Three - Two Parameter Constructor");
            System.out.println("Size: " + size);
           System.out.println("Toppings: " + toppings);
           System.out.println("");
        }
        
        public Pizza(String setSize)
        {
            setSize("Medium");
            setToppings("Regular");
            System.out.println("Pizza Two - One Parameter Constructor");
            System.out.println("Size: " + size);
            System.out.println("Toppings: " + toppings);
            System.out.println("");
        }
        
        public void setSize(String size)
        {
            this.size = size;
        }
        
        public void setToppings(String toppings)
        {
            this.toppings = toppings;
        }
        
        public String getSize()
        {
            return this.size;
        }
        
        public String getToppings()
        {
            return this.toppings;
        }
        
        public static int getPizzasSold()
        {
            return PizzasSold;
        }
        
       // public boolean equal(Pizza CompPizza)
       // {
          //  if(this.getSize()==(CompPizza.getSize()))
           // {
             //   if(this.getToppings()==(CompPizza.getToppings()))
             //   {
              //      return true;
                //}
              //  else
               // {
               //     return false;
              //  }
           // }
          //  return false;
       // }
        
              public boolean equal(Pizza CompPizza)
        {
            if(this.getSize()==(CompPizza.getSize()))
            {
                if(this.getToppings()==(CompPizza.getToppings()))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
            private void calculatePrice()
        {
    
        }
        
        
        public void display(String size, String toppings, double price)
        {
            System.out.println("Size: " + size);
            System.out.println("Toppings: " + toppings);
            System.out.println("Price: " + price);
            
        }
        
    
        
    
    }
    Code:
    import java.util.Scanner;
    
    /**
     *Has Pizzas
     *
     * @author Michael Wojtysiak
      * @version 1.1
     */
    public class OrderForm
    {
    public static void main(String args[])
        {
    
            
                String mySize = args[0];
                String myToppings = args[1];
              
    
    
                CheckOrder.checkSize(args[0]);
               // CheckOrder.checkTopping(args[1]);
              
               Pizza One = new Pizza();
               Pizza Two = new Pizza(mySize);
               Pizza Three = new Pizza(mySize,myToppings);
              
    
                
                
               boolean Res = Three .equal(One);
               if(Res == true)
               {
                   System.out.println("They are the same!");
                }
                else
                {
                    System.out.println("They are different!");
                }
                
            
    
    
     
        }
    }
    Code:
    /**
     * Write a description of class CheckOrder here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class CheckOrder
    {
       public static boolean checkSize(String size)
       {
               char num = size;
            String checksize;
    
            switch(num)
            {
                case 's' :   checksize = "Small";
                           break;
                case 'm' :   checksize = "Medium";
                           break;
                case 'l' :   checksize = "Large";
                           break;
                default  :     checksize = "Invalid";
                
            }
    
            if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large"))
            {
                System.out.println("Your Size is: " + checksize);
            }
                
    
            else
            {
                System.out.println("Invalid Size");
            }
          
          
    
          
    
        }
    
               //if(boolean checkSize = true)
               //{
                 //  size = "Small";
               // }
                //else
                //{
                //    System.out.println("Invalid");
               // }
            
        
    
      
    }
    Attached Images
    File Type: pdf assignment5.pdf (77.9 KB, 23 views)
    Reply With Quote
      #2    
    Old November 6th, 2009, 07:13 PM
    Xeel's Avatar
    Xeel Xeel is offline
    Member
     
    Join Date: Jul 2005
    Location: Currently in Mexico City
    Posts: 465
    Xeel has a spectacular aura about (150+) Xeel has a spectacular aura about (150+)
    Re: Java Pizza Program Help

    The code itself makes little sense, but anyways...

    The things I saw in the first 5 seconds:

    error#1: char num = size;
    You can't initialize char with a string directly. One of many ways to do it would be char num = size.charAt(0); considering you are sure there is at least one char in the string.

    error#2: if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large")), this.getSize()==CompPizza.getSize() and more...
    That's a nice one =). This is no JavaScript, you can't compare objects' contents the way you do with primitives. Use equals() method for this (i.e. str1.equals(str2)).

    And I am not even talking about algorithm corrections/optimizations... The program will throw basic exceptions almost every line if something goes wrong. Check status of one step before starting the next one.
    __________________
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    willCodeForFood("PHP,Java,C#,C++,Assembler,XML,VBS,XHTML,CSS,JavaScript,SQL"); //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

    Last edited by Xeel; November 6th, 2009 at 07:16 PM.
    Reply With Quote
      #3    
    Old November 6th, 2009, 07:58 PM
    mojoopop mojoopop is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    mojoopop is an unknown quantity at this point (<10)
    Re: Java Pizza Program Help

    ok number 1 was delt with what do you suggest about error 2... and ye the codes probably incorrect in many ways but please note i am a first year college student with no background in any programming and i am only a month in my object oriented java 1 program
    Reply With Quote
      #4    
    Old November 8th, 2009, 02:32 PM
    Deliverance Deliverance is offline
    Member
     
    Join Date: Apr 2007
    Posts: 369
    Deliverance is on a distinguished road (40+)
    Re: Java Pizza Program Help

    Quote:
    Originally Posted by Xeel View Post
    The code itself makes little sense, but anyways...

    The things I saw in the first 5 seconds:

    error#1: char num = size;
    You can't initialize char with a string directly. One of many ways to do it would be char num = size.charAt(0); considering you are sure there is at least one char in the string.

    error#2: if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large")), this.getSize()==CompPizza.getSize() and more...
    That's a nice one =). This is no JavaScript, you can't compare objects' contents the way you do with primitives. Use equals() method for this (i.e. str1.equals(str2)).

    And I am not even talking about algorithm corrections/optimizations... The program will throw basic exceptions almost every line if something goes wrong. Check status of one step before starting the next one.
    I want to add a suggestion to the OP and make it more explicit what Xeel is saying. I very seldom see people doing this, and maybe they're just used to it or just followed one bad programmer's example, but consider this when doing string comparisons:

    Code:
    String s = null;
    if(s.equals("Something")) {
      // NullPointerException
    }
    if("Something".equals(s)) {
      // Will never NPE
    }
    Always use the latter approach of comparing a string literal against a reference object when you can.
    __________________
    ------
    If you are satisfied with the responses, add to the user's rep!
    Reply With Quote
    Reply

    Bookmarks

    Tags
    pizza
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:53 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.