Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
ohstar1204

Need someone to help me to do create this java

7 posts in this topic

Im really struggling and don't know how start and do it probably , may someone who have done this or good at java help me this one please ? post-234357-0-68964900-1432180455_thumb.post-234357-0-91203200-1432180457_thumb.post-234357-0-73610200-1432180556_thumb.

Share this post


Link to post
Share on other sites

public class Product 

private double itemPrice; 


public Product(double productType) 

itemPrice = productType; 




public void setItemPrice(double productType) 

if(productType == 1 ) 

productType = 345; 


public double getItemPrice(double productType) 


return productType; 


public String toString() 

String myString = " " + itemPrice; 
return myString; 


What's wrong with this ? How can i fix it and create other prices for other product ?

 

Share this post


Link to post
Share on other sites

I would do

public class Product { private double itemPrice; public Product(double price) { this.itemPrice = price; } public void setItemPrice(double productType) { this.itemPrice = productType} public double getItemPrice() { return this.itemPrice; } public String toString() { String myString = "$" + this.itemPrice; return myString; } 

This will store the price of a product in an object and you can get it, set it, and toString it

 

You create a product for example with a line like this

Product ipod = new Product(299.99);Product iphone = new Product(499.99);

Share this post


Link to post
Share on other sites

Hey there, I'd like to chime in on this (Entirely new to the website). I was/am the Computer Science tutor at my college and there are a few things that I used to break projects down for people.

 

1. The best thing to do is to always create an algorithm or structure chart of some type (Something like a UML but it can be totally sketched out/text based).

Quick rundown of the program:

  • Driver Class: Instantiates products with names/prices. It will then output a set of information to the console and ask for user input (With the scanner).

 

  • Product Class: Receives the information from the driver class and "assigns it"

 

  • Change Class: When called, you will pass through the total cost/tendered amount and it will do the calculations. Or you will pass through the total cost and the Change Class could manage the user input for the tendered amount. However, I would suggest keeping user input within the main method.

 

                                                                                                                                                                                                        

 

 

Now that we have a bases of how the program should operate, we can begin to look at the code itself. This isn't something that I can just write for you, but something that I can help guide you in the right direction (Same thing that NewBotterFTW said).

 

One thing I noticed in the code you posted above was that you were declaring the price of the product within the default constructor. It should actually be the name of the product as that is what the assignment description calls for. You are also missing the get/set total price  and the getName methods for the Product class. 

 

Regarding the default constructor and how it should be the name instead of the price:

public class Product { /*We must declare the variables outside of the default constructors/methods to be used within*   the entire class.**Each time we create a new product object (Product socks = new Product("Socks"),*   they will each have their own "unique" productName and itemPrice.**   Remember:*     Product socks = new Product("Socks");*     Product boots= new Product("boots");**     Although socks and boots are both Product objects, they are individually unique.*     socks.getName() would return the productName of socks - "Socks"*     boots.getName() would return the productName of booys - "boots"**/private double itemPrice; private String productName;public Product(String nm) { //Here, we must say this.productName instead of just productName.// This is because we must assign the string to the "global" variable productName instead of the local one (Look up scopes of variables).this.productName = nm; } }

Once you have made this change, creating a "Product object" within the driver class is pretty easy.

Product socks = new Product ("Socks");socks.setPrice ( 200 );

Remember that for the assignment, it asks for everything in cents. So 200 = $2.

You will have to complete a easy/simple math calculation in order to make your final output change it from cents to dollar format.

Edited by Eggs
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.