• 0

Stuck in a Java question


Question

Hey,

So,I'm a newbie to Java,I have an assignment and I'm stuck,I know online there's a lot of paid services who'd solve this for me,I don't want the answer.i've been trying to understand this for while.

-I have to use the toString method and have a test class.

-My problem is I don't understand how we'd update the invoice using just one constructor,and  a set method(in short,how to make the output appear like it should).I Feel that what I did is wrong.

-Original Question ,output both mine and the how it supposed to be are the attachments.

 

First class:

public class Invoice {

        private String PartNumber;
      private String Description; 
      private int Quantity ;
     private double Price ;
     
     public Invoice(String PartNumber,String Description,int Quantity,double Price){
         this.PartNumber=PartNumber;
         this.Description=Description;
         this.Quantity=Quantity;
         this.Price=Price;
       
     }
     public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }
     
      public void setPartNumber(String PartNumber){
          this.PartNumber=PartNumber;
      }
      
      public void setDescription(String Description){
          this.Description=Description;
      }
      
      
       public void setQuantity(int Quantity){
          this.Quantity=Quantity;
      }
         public void setPrice(double Price){
          this.Price=Price;
      }
      
      public double getInvoiceAmount(){
          return Quantity*Price;
      }
      
      public String toString(){
             return String.format("Part Number:%s\nDescription:%s\nQuantity:%d\nPrice:%.2f\nInvoice Amount:%.2f",PartNumber,Description,Quantity,Price,getInvoiceAmount());
             } 
     
        public void display(){
            System.out.println(toString());
        }
    }

Capture.PNG

Capture22.PNG

Myoutput.PNG

Second class:


public class InvoiceTest {
    public static void main(String[] args) {
Invoice a1=new Invoice();
a1.display();
Invoice a2=new Invoice();
a2.setDescription("Yellow Hammer");
a2.setPrice(19.49);
a2.setQuantity(3);
a2.display();
Invoice a3=new Invoice("5678"," Paint Brush",0,0.0);
        a3.display();
        Invoice a4=new Invoice("5678"," Paint Brush",0,0.0);
        a4.setQuantity(3);
        a4.setPrice(4.94);
        a4.display();


    }
}
 

 

Link to comment
https://www.neowin.net/forum/topic/1358584-stuck-in-a-java-question/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

When they say "updated invoice" they mean you should modify an existing Invoice, not create a new one.

So in total you're going to call the constructor twice in your program.

To update each invoice you're going to use the `set..` methods and then call .toString() a second time on that same instance.

 

Your Invoice class should NOT have a default constructor (constructor with no parameters) that initializes it to the values of the first invoice example. You should use the constructor that takes in those initial values and assigns them immediately. And just delete the other one.

 

// Delete this code!!
public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }

 

Does that make sense?

  • 0
  On 15/03/2018 at 17:31, Andre S. said:

When they say "updated invoice" they mean you should modify an existing Invoice, not create a new one.

So in total you're going to call the constructor twice in your program.

To update each invoice you're going to use the `set..` methods and then call .toString() a second time on that same instance.

 

Your Invoice class should NOT have a default constructor (constructor with no parameters) that initializes it to the values of the first invoice example. You should use the constructor that takes in those initial values and assigns them immediately. And just delete the other one.

 

// Delete this code!!
public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }

 

Does that make sense?

Expand  

Thank you homie.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • "While users may say they do not want their data used for personalized ads, Meta believes that without personalization, user experience declines with an almost 800% rise in ads being marked as “irrelevant” or “repetitive”. The ads might be more irrelevant, but it's not like people crave ads in the first place. My user experience with ads isn't better with personalization, because I don't want them there to begin with. So I might as well have non-personalized ads if I am gonna have ads, because then I at least get tracked less, and that makes it a better user experience for me.
    • The fact that they didn't offer a non-personalized ad-supported option, when they were mandated by law, was the final nail in the coffin in my case.
    • Vivaldi 7.5.3735.41 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Download: Vivaldi 64-bit | 125.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot | Release Annoucement Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • the 9070s have only been recently released. Finewine is more about what performance improvements exist in OLD cards. So... what are the performance improvements in the 7900XTX?
  • Recent Achievements

    • Week One Done
      BeeJay_Balu earned a badge
      Week One Done
    • Week One Done
      filminutz earned a badge
      Week One Done
    • Reacting Well
      SteveJaye earned a badge
      Reacting Well
    • One Month Later
      MadMung0 earned a badge
      One Month Later
    • One Month Later
      Uranus_enjoyer earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      444
    2. 2
      ATLien_0
      161
    3. 3
      +FloatingFatMan
      147
    4. 4
      Nick H.
      65
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!