• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on code class and calculations  RSS feed

 
seke mutema
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been given the following work

As a software engineer for a building contractor, you have been tasked with computerizing the building contractor’s business operations. The building contractor has many clients, each requiring a specific job to be carried out. In pricing each job, the materials used, the quantity of materials and the labour cost are considered. You have chosen the object-oriented approach in designing a suitable data management system.
Produce an object-oriented system with a Material class that represents any Material to be used for jobs, a class Job that details all the material required for a particular job and associated costs, and a class DataMgt that captures the lists of all Materials, all Jobs and presents the main system interaction of all system objects. The object-oriented system contains the following:
(1) A class Material containing
a. A private property descrip for the description or name of the material.
b. A private property unitWeight for the unit weight of the material.
c. A private property unitPrice for the price of each unit of material.
d. A constructor which accepts parameters.
e. Accessor methods for all private properties.

here is my code. Am I right

Is this the correct answer thank you. I am new and I need better ideas and better methods to code
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, welcome to the Ranch!

Second, below are my comments.

Grand Mutondo wrote:Produce an object-oriented system with a Material class (...)

You don't have Material class. You do have material class. Java is case-sensitive.

Grand Mutondo wrote:(...) a class Job (...) and a class DataMgt (...)

You don't have those classes. I guess you are going to write them later.

Grand Mutondo wrote:A class Material containing (...) accessor methods for all private properties.

You don't have these.

  • Your Material class should not have main method. Read about it here: http://www.coderanch.com/t/660020/Wiki/Main-Pain
  • Your Material class should not be responsible for handling user's input. That is a job for DataMgt class.
  • Don't use JOptionPane. This is an old-fashioned way to get input.
  • Use meaningful variable names. Not d, w or p like on line 16.
  • On line 22-24 you declare variables that do nothing. Also, there's no need to multiply by 1.
  • Method input() is not an accesor. An accessor would looke like:
  • You need to indent your code better.
  • I don't think you need a parameterless constructor.
  • The constructor should have public access.
  • Your unitWeight field is an integer. What if something weights 1.5 kg?
  •  
    Knute Snortum
    Sheriff
    Posts: 4270
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch.

    Here are thoughts about your code:

    * Indentation is as important as correct syntax.
    * Accessor methods (also called "getters") are used to access the the private fields, not input data. In general, they look like this:

    * Class names should start with an Upper Case Letter.
    * You have a constructor with a comment saying it takes parameters, when it only sets defaults.
    * The next constructor does take parameters, but it sets three variables that do nothing. These variables are gone as soon as the constructor is done.
     
    Campbell Ritchie
    Marshal
    Posts: 56520
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have doubtless been told that constructors are there to enable the class to be instantiated to an object. But that is only half the story. You write a constructor to restrict instantiation. There are various ways you can restrict things. One is to make the only constructor private, and as it says in the Java® Language Specification (=JLS), you cannot instantiate the class at all. That is one of the few parts of the JLS that are anything like easy to read. That is probably not what you want at the moment, but remember it because one day you will want it.

    Another thing you can do is write a constructor requiring parameters. That means you are telling the world that if you want an object of this class, you can have it, but you must pass certain information. So you want a constructor taking description weight and price. There are ways you can validate the input, but that is probably too complicated for you now. But you will have values. So you don't want a constructor which allows you to set the values to 0. So you don't want the no‑arguments constructor (unless your instructions tell you to). you should delete that no‑arguments constructor. Remember the more access there is into your object, the more opportunity there is for people to do something wrong with it.
     
    seke mutema
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for your help and corrections. It really helped me to improve on my coding, but the problem my code is not connecting with the other classes. I did post the first section and the code that i had done here is the second section and the code including the improved section A as per your comments.

    (2) A class Job containing
    a. A property descrip for the description or title of a job.
    b. A property price for the total price of the job.
    c. A property materials for storing the list of materials required for the job.
    d. A constructor which accepts parameters.
    e. A method addItem() that adds an item/material passed as a parameter to the list of materials required for the job.
    f. A method calcPrice() that calculates the total price of a job.
    g. A method getPrice() that returns the total price of a job.
    (3) A class dataMgt containing
    a. A property materialList for storing the list of Material objects.
    b. A property jobList for storing the list of all Job objects.
    c. A constructor that does not accept any parameters.
    d. A method processJob() that accepts a job request from the user; processJob() then uses the job request information to calculate the total price of a job and then prints the appropriate information to a dialog box or to a console.
    e. A main() method that creates a dataMgt object called dm1 and then invokes the method processJob() on the object dm1.


    job class code



    Material class code




    dataMgt class


    and the Main class

     
    Paweł Baczyński
    Bartender
    Posts: 2074
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your main method does nothing.
    How do you expect anything to happen?
    How do you run your program?
     
    seke mutema
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have tried my best, but the problem my Job class which inherits from Material class is failing to access the attributes declared as private but i do have the getters and setters in A. My code is like this now;
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    seke mutema wrote:My code is like this now...

    I've corrected your code tags. You have to first select the code you want to tag and then hit the 'Code' button.

    But your indenting is still REALLY bad.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    seke mutema wrote:I have tried my best, but the problem my Job class which inherits from Material class]

    Question: Why do you think Job should extend Material? It doesn't sound right to me.

    Winston
     
    Paweł Baczyński
    Bartender
    Posts: 2074
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, I don't think think dataMgt should extend Job.
    By the way, this class should be named DataMgt. Remember Java is case sensitive.
     
    Knute Snortum
    Sheriff
    Posts: 4270
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are missing a close parenthesis, probably somewhere about void output(). This is why indentation is so important.

    You seem to have problems with capitalization. As was mentioned before, Java is case sensitive. Classes should start with an uppercase letter; variables and methods should start with a lowercase letter.

    It's hard to say, but you seem to be mixing up method names and variable names. A method is always called with parentheses, even if it's void: myMethod().
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!