?
Solved

Object Oriented

Posted on 2006-03-19
31
Medium Priority
?
566 Views
Last Modified: 2013-11-23
HI. I am up late doing homework and I am almost done with all the hard stuff with this app I just wrote.  Please dont laugh at my code, I am just learning Java.  With this program you input the price for cereal and milk at store a and b.  the answer comes back @ cereal *3 and milk * 2's total from the store that has the cheapest price.  Now I did this one in the "main" way, however I dont know heads or tail about doing the same thing the "Object Oriented way".  Im sure this would be quite simple for you guys and this question is quite laughable, but for me im frustrated.  Any help?


import javax.swing.*;

public class Store1 {

     
          public static double getSum(double one, double two)
     {
           return one + two;
     }

     public static void main(String[] args) {
               
     double value1;
     String input1 = JOptionPane.showInputDialog("Store A \n Input the price of a box of cereal");
     value1 = Double.parseDouble(input1);

               
     double value2;     String input2 = JOptionPane.showInputDialog("Store A \nl Input the price of a quart of Milk");
     value2 = Double.parseDouble(input2);

               
     double value3;
     String input3 = JOptionPane.showInputDialog("Store B \n Input the price of a box of cereal");
     value3 = Double.parseDouble(input3);

               
     double value4;
     String input4 = JOptionPane.showInputDialog("Store B \n Input the price of a quart of milk");
     value4 = Double.parseDouble(input4);

     double producta = getSum(value1*3, value2*2);
     double productb = getSum(value3*3, value4*2);

     if(value1 + value2 < value3 + value4){

          JOptionPane.showMessageDialog(null, "Store A Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + producta);

          }

     if(value1 + value2 > value3 + value4){

          JOptionPane.showMessageDialog(null, "Store B Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + productb);

          }    
     

     }

}
0
Comment
Question by:Devario Johnson
  • 14
  • 8
  • 6
  • +2
31 Comments
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16233216
Are you looking for how to do this Object Oriented?  I didn't try your code but I read through it and I saw no blatant mistakes so it should work, so what do you need help with.

If its making it OO, I think in an instance like this it would actually be harder because it would be like 2-3 classes depending how you did it.  If you don't understand OOP their are a ton of guides on the internet that you can browse through.

Just a little thing though, if I were to do this OO, I would make each cereal box an object and each milk an object, and each of them would have two parameters, a price and a store, and both price and store can be called from anywhere and are unique to that object.  OOP generally makes stuff easier, like for example if you were comparing the prices of every item in every store in your county, but in a program as small as this I just think its too much work, but good practice.  Post if you need more help.

Mark
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233234
yeah I do, I am trying to get it OO, but I dont even know where to start, you say instance and I dont even know what you mean.  honestly I just started like a few hours 11 am this morning.  :)  Too bad this crap is due tommorow!
0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16233246
You really shouldn't say so much that this is homework, we aren't supposed to help with homework.  But I will give you a little help just give me some time to type it up.

Also you may want to think about this, run your program, for Store A use 1.75 for Cereal and 1.65 for Milk, then for Store B use 1.65 for cereal and 1.75 for Milk.

Perhaps since you are getting 3 of one product and 2 of another you should have the if statments be if "producta > productb" and vice versa.

Mark
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16233256
import javax.swing.*;

public class Store1 {

     
          public static double getSum(double one, double two)
     {
           return one + two;
     }

     public static void main(String[] args) {
               
     double value1;
     String input1 = JOptionPane.showInputDialog("Store A \n Input the price of a box of cereal");
     value1 = Double.parseDouble(input1);

               
     double value2;     String input2 = JOptionPane.showInputDialog("Store A \nl Input the price of a quart of Milk");
     value2 = Double.parseDouble(input2);

               
     double value3;
     String input3 = JOptionPane.showInputDialog("Store B \n Input the price of a box of cereal");
     value3 = Double.parseDouble(input3);

               
     double value4;
     String input4 = JOptionPane.showInputDialog("Store B \n Input the price of a quart of milk");
     value4 = Double.parseDouble(input4);

     double producta = getSum(value1*3, value2*2);
     double productb = getSum(value3*3, value4*2);

     if(producta< productb){

          JOptionPane.showMessageDialog(null, "Store A Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + producta);

          }

     if(producta > productb){

          JOptionPane.showMessageDialog(null, "Store B Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + productb);

          }    
     

     }


check this code
0
 
LVL 1

Accepted Solution

by:
simpsons17371 earned 500 total points
ID: 16233306
Ok, here is your main class:

------------------------------------------------------------------
import javax.swing.*;

public class Store1
{
      public static void main(String[] args)
    {
          double ca;
          String input1 = JOptionPane.showInputDialog("Store A \n Input the price of a box of cereal");
           ca = Double.parseDouble(input1);

            double ma;
           String input2 = JOptionPane.showInputDialog("Store A \nl Input the price of a quart of Milk");
           ma = Double.parseDouble(input2);

            double cb;
           String input3 = JOptionPane.showInputDialog("Store B \n Input the price of a box of cereal");
           cb = Double.parseDouble(input3);

            double mb;
           String input4 = JOptionPane.showInputDialog("Store B \n Input the price of a quart of milk");
           mb = Double.parseDouble(input4);

            Cereal storea = new Cereal(ca);
            Cereal storeb = new Cereal(cb);
            Milk milkstorea = new Milk(ma);
            Milk milkstoreb = new Milk(mb);

            double costa = storea.getPrice()*3 + milkstorea.getPrice()*2;
            double costb = storeb.getPrice()*3 + milkstorea.getPrice()*2;

            if(costb > costa)
            {
                  JOptionPane.showMessageDialog(null, "Store A Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costa);
            }
            else if(costa > costb)
            {
                  JOptionPane.showMessageDialog(null, "Store B Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costb);
            }
            else
            {
                  JOptionPane.showMessageDialog(null, "Both of the stores have equal prices.  \n Three boxes of cereal and two quarts of milk cost: $" + costa);
            }
      }
}


------------------------------------------------------------------

This is your Cereal class

------------------------------------------------------------------
public class Cereal
{
      protected double price;
      
      public Cereal(double cost)
      {
            price = cost;
      }
      
      public double getPrice()
      {
            return price;
      }
}


----------------------------------------------------------

And this is your Milk class
----------------------------------------------------------

public class Milk
{
      protected double price;
      
      public Milk(double cost)
      {
            price = cost;
      }
      
      public double getPrice()
      {
            return price;
      }
}

-----------------------------------------------------------


I spliced in a lot of your code so that you might have an easier time following it, if you have more questions post again.

Mark
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233310
>> you say instance and I dont even know what you mean

An object of a class :)

Maybe you need a basic tutorial to brush-up: http://java.sun.com/docs/books/tutorial/
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233320
oh I see what you are saying about the product a > product b thing.  I alread yhad it declared huh, but im just happy that it works for now, it's been a struggle.  Thanks for doing the oo for me...can u make sure and leave a comment or 2 so that I can know how to convert others to OO, im gonna be up allllll night...thanks again.
0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16233345
I'm not much of a teacher so its hard for me to explain stuff especially OOP.  There are great tutorials around and stuff, but you just have to start learning to thing in terms of objects and OOP and everything will come so much easier.

For example, before I even looked at your code I though, in this case I would have each cereal item its own object and each milk items own object.  By doing this, you can have multiple types of Cereals all sharing similar properties.  This is very simple example and we only needed price, but lets think more complex, we could have instead of Cereal(price), we could have Cereal(brand, name, maker, store, price, total calaries, total fat, ingredients, serving sizes, etc..), so in more complex examples it would be practical to have OOP behind everything.  Using it can instead of having a bunch of variables repeating themselves for every type of cereal and milk, you make every cereal and milk an object, and these objects hold your variables and all the values for you, and they are always saved, and you can change them whenever, and it makes it easy to access.

The bigger the program gets the more objects you can have, lets say you want to make a poker game, you COULD make a billion and two variables for everything you need.  Or you could make a Card class, and every card of a 52 card deck would be a Card object, and then 52 cards make up another object class called Deck, and like 5 decks would make up one Table.  Then there are 10 Players who all have properties such as seat number, bank account, bet, cards they have, etc.  You could go on more and more in this example, but you see you would have objects in objects in objects calling other objects and you would have no need for a ton of variables.

Its so hard to explain this, I'm just trying to give you ways you can simplify work with OOP, I hope this has helped, I cannot post agian until tomorrow because I am tired and hitting the hay.  Good luck with everything and I hope you can use this is program and thread as a base for all your OOP needs in the future.

Mark
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233349
so...in covnversion to OO, I need to make anything that is doing some thing it's seperate page, but also have a main page with all the code?
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233354
wow thank you so much man, you are great.  Here are your points, I hope you make a poker site in the future...;)  that could make you alot of money.
0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16233365
Hmm, no not necessarily.  There really is no rule of when to use and when not to use OOP.  An input from a user wouldn't have to be OO, but what you use that input for might have to be.  For example, from the previous example, the price for milk and store a I made, ma.  That is a double value recieved from a user, it doesn't have to be an object.  But we used it to make an object.  Say you were making an Aquarium, you might ask the user how many fish you want, he would say 10.  This value of 10 is not an object, but the ten fish you make because of it are objects.

Hope this helps.

Mark
0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16233371
Thanks for the points, also, I at one point in the summer tried to do a poker site with my brother, I really couldn't get that far, its like impossible, lol, don't even try unless you have a doctorate in programming, lol.

Mark
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233441
yea, ive been thinking about doing one, but hiring people to be partners to do one...im good with flash so I could to the visuals  and let them work on all the back end stuff
0
 

Expert Comment

by:arjunsa2z
ID: 16233626
Hi folks,
                 I have changed the code of "mayankeagle" and tried to implement some concepts of OOPs into the program.

import javax.swing.*;

public class StorePrice{
      
       private double storeA_Cereal;
       private double storeA_Milk;
       private double storeB_Cereal;
       private double storeB_Milk;
      
       public void getInputs(){
             String input1 = JOptionPane.showInputDialog("Store A \n Input the price of a box of cereal");
        storeA_Cereal = Double.parseDouble(input1);

        double ma;
        String input2 = JOptionPane.showInputDialog("Store A \nl Input the price of a quart of Milk");
        storeA_Milk = Double.parseDouble(input2);

        double cb;
        String input3 = JOptionPane.showInputDialog("Store B \n Input the price of a box of cereal");
        storeB_Cereal = Double.parseDouble(input3);

        double mb;
        String input4 = JOptionPane.showInputDialog("Store B \n Input the price of a quart of milk");
        storeB_Milk = Double.parseDouble(input4);
      }
      
      public void getResult(){
            double costA = storeA_Cereal*3 + storeA_Milk*2;
        double costB = storeB_Cereal*3 + storeB_Milk*2;

        if(costB > costA)
        {
             JOptionPane.showMessageDialog(null, "Store A Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costA);
        }
        else if(costB > costB)
        {
             JOptionPane.showMessageDialog(null, "Store B Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costB);
        }
        else
        {
             JOptionPane.showMessageDialog(null, "Both of the stores have equal prices.  \n Three boxes of cereal and two quarts of milk cost: $" + costA);
        }
      }
      
        public double getstoreA_Cereal(){
            return storeA_Cereal;
      }
      
      public double getstoreB_Cereal(){
            return storeB_Cereal;
      }
      
      public double getstoreA_Milk(){
            return storeA_Milk;
      }
      
      public double getstoreB_Milk(){
            return storeB_Milk;
      }
    public static void main(String args[]){
            StorePrice storePriceEstimation = new StorePrice();
            storePriceEstimation.getInputs();
            storePriceEstimation.getResult();
    }
      
}

I declare the variable as Private class variables this will give the protection to the data, avoiding miss use. The entire functionality is divided into module for easy maintainance of code. U can create the object and use the functionality where ever u want.

Regards,
Arjun M.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233633
>> I have changed the code of "mayankeagle"

You mean simpsons :) ?
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233658
ummm so are you saying this will run toally standalone of external classes?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233673
It needs the Swing classes.
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233677
remember im new at this being as simple as possible would be great
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233741
Well, all you need is the standard Java Runtime Environment and nothing external.
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233748
what i am referring to is the fact taht he didnt have a main class in the header...could this still work
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233766
I see a public static void main(String args[]){

in arjun's code?
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233770
oh it's oin the bottom...I dint know you could do that ...cool seems so compicated
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233781
Sure, you can put a method (function) anywhere inside the class - the order of methods does not matter.
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233795
nah i was referring to the fact that is was OO and it was all still just one .java
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233800
quick question...what is wrong here???

import javax.swing.*;

public class Inflation {

      public static void main(String[] args)
{

      double value;
      String input = JOptionPane.showInputDialog("Plese enter an amount to be \n inflated over a 3 year period.");
      value = Double.parseDouble(input);

      double inflated = value*.16;

      JOptionPane.showOutputDialog(null, "Here is your inflated total " + inflated);

      }

}
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233823
ok, I got it...:) i missed something small, now I gatta turn this to OO
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16233828
import javax.swing.*;

public class Inflation {

      public static void main(String[] args)
{

      double value;
      String input = JOptionPane.showInputDialog("Plese enter an amount to be \n inflated over a 3 year period.");
      value = Double.parseDouble(input);

      double inflated = value*.16;
      double total = inflated + value;

      JOptionPane.showMessageDialog(null, "Here is your inflated total: $" + total);

      }

}
0
 

Expert Comment

by:arjunsa2z
ID: 16234091
Hi folks,
         I think every one is femiliar with OOPs. Here any of the problems devarioj asked dont need OOPs.
      But as he/she ask it to be converted to OOPs version.

     This same program we can write in c/c++/java/VB(what i know) etc.,The main advantage of OOPs is, its easy to maintain and securied. Let me explain one after other,
   
         He said He/she what a program, lets take this as a He/she what a class which the required work.And he want to use the class in where ever project he can.

         1. I declared/defined my variables as Private Class variables. This means these variables can only be access by inside the code. The advantage of this is, variables cant be modify externally but can be accessed.

i have to use the class i defined in some other class ABC,

public class ABc{
    public static void main(Stirng args[]){
        StorePrice xyz = new StrorePrice();
        xyz.getInputs();
        xyz.getResult();
        System.out.println("Store A Milk price :"+xyz.getstoreA_Milk);
    }
}

I think this concept is called Data Hiding and Encaptulation.

There is no chance for inhertance, polymorphism.
With same changes we can even implement them also.

And if simpsons17371  dont mind, can i ask, what is OOPS in the accepted program, in C we can use scanf/printf and do the same. But the logic of simpsons17371 is recommended for simple programs like this.

Thanx,

Regards,
Arjun M
0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16244378
>>>
And if simpsons17371  dont mind, can i ask, what is OOPS in the accepted program, in C we can use scanf/printf and do the same. But the logic of simpsons17371 is recommended for simple programs like this.
>>>

The "OOP's" as you call them, in the program that I submitted, are Cereal and Milk.  Both of those are object types.  When the price is recieved from the user, it uses that price to make the object with a parameter of the price.  So it starts off with Cereal of Store A.  The user inputs the price, and an object is created, called storea, the object type being Cereal, which just means that it must have one set of parameters that equals a declaration function in the Cereal class, and the parameters of this one is just price.  So it ends up that there are four objects, two Milk, and two Cereal, and each have just one parameter or variable, which is their price.  So the reason that it is object oriented is because those are all objects.

As for the program that you posted, it is not OOP at all.  What you do, just in a different manner than that of devarioj originally did was get the values from the user, and then save them as double variables.  What OOP does is take that a step further and take those double values and store them forever in an object, and thats what I did in mine.

Also, devarioj:

>>>
nah i was referring to the fact that is was OO and it was all still just one .java
>>>

It really wasn't OO, but it can all be in one file, like so:

------------------------------------------------------------------

import javax.swing.*;

public class Store1
{
     public static void main(String[] args)
    {
         double ca;
         String input1 = JOptionPane.showInputDialog("Store A \n Input the price of a box of cereal");
          ca = Double.parseDouble(input1);

          double ma;
          String input2 = JOptionPane.showInputDialog("Store A \nl Input the price of a quart of Milk");
          ma = Double.parseDouble(input2);

          double cb;
          String input3 = JOptionPane.showInputDialog("Store B \n Input the price of a box of cereal");
          cb = Double.parseDouble(input3);

          double mb;
          String input4 = JOptionPane.showInputDialog("Store B \n Input the price of a quart of milk");
          mb = Double.parseDouble(input4);

          Cereal storea = new Cereal(ca);
          Cereal storeb = new Cereal(cb);
          Milk milkstorea = new Milk(ma);
          Milk milkstoreb = new Milk(mb);

          double costa = storea.getPrice()*3 + milkstorea.getPrice()*2;
          double costb = storeb.getPrice()*3 + milkstorea.getPrice()*2;

          if(costb > costa)
          {
               JOptionPane.showMessageDialog(null, "Store A Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costa);
          }
          else if(costa > costb)
          {
               JOptionPane.showMessageDialog(null, "Store B Has lower prices. \n Three boxes of cereal and two quarts of milk cost: $" + costb);
          }
          else
          {
               JOptionPane.showMessageDialog(null, "Both of the stores have equal prices.  \n Three boxes of cereal and two quarts of milk cost: $" + costa);
          }
     }
}

class Cereal
{
     protected double price;
     
     public Cereal(double cost)
     {
          price = cost;
     }
     
     public double getPrice()
     {
          return price;
     }
}

class Milk
{
     protected double price;
     
     public Milk(double cost)
     {
          price = cost;
     }
     
     public double getPrice()
     {
          return price;
     }
}

That is OOP, it is three seperate classes, but it is all in one file.  I never do this, I just think that it gets too clustered up and is hard to find everything, I think seperate files for seperate classes just makes sense.

Mark

0
 
LVL 1

Expert Comment

by:simpsons17371
ID: 16244395
Wow I would like to make an edit, I think what you made Arjun, is OOP.  I misread it and was rushing through reading trying to catch up and post and didn't see what you truly had written down.  What you did is also OOP because you made an object, a StorePrice object called StorePriceEstimation.

So that is object oriented programming, just in a different way.  I use that way when I have like 5 or 6 classes and its a fairly big program, because using that you can make your static main class be a non static main class and you will end a lot of problems that arise with the whole static non-static issues of higher programming.

Mark
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 16244646
hmm, I see.  Thanks for the input, I spokt to my teacher today and he said that OOp is all about having the main contain input and output to the user and the other classes and such be on different pages...is this correct?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question