• 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

    • Maybe you don't realize this...but everything you said agreed with me. Yes, many tech outlets reported on Ryzen 9000 issues prior to 24H2, which I already addressed, and as I already said, that issue only existed for a few short months. Ryzen 9000 was released the same quarter of 2024 as 24H2. So again...months, not years. I also already said 24H2 showed some minor improvements on older Ryzen CPU. The article you posted agrees with me, and even says the improvements were only 3-5%, which is even more harsh a statement than what I said. If you want to fuss on the 3-5% numbers, then yes, I will grant you that was an issue for an extended amount of time. In my opinion, that is such a small amount it isn't worth fussing over, but you are welcome to a different option. However, if that was your point, then you didn't make that point in good faith, because you highlighted Ryzen 9000 so much, which had a FAR bigger and FAR shorter issue, it's really a very different conversation.
    • The vast majority of users do not care which iOS version they're using. They don't even know or bother updating to the latest version, unless they see the prompt. The version numbers is more for the power users and I don't think Apple renamed their OS for them.
    • No operating system works well with programs that modify its interface. There will always be bugs, whether it's MacOS or Linux. Windows too, you'll only suffer from bugs using this crap software.
    • KDE brings UI improvements, bug fixes and more to Plasma 6.4 as stable release draws near by David Uzondu With less than a month to the release of Plasma 6.4, the KDE dev team has shared what it has been working on in the latest issue of its weekly roundup. The update shows a heavy focus on user interface polish and a whole slew of bug fixes as the June 17 release date gets closer. The team has pushed a number of UI refinements for the upcoming version. On the System Settings page for Wi-Fi, the network list can now be fully navigated with a keyboard. KDE also disabled the ability to drag and drop displays on top of one another in the monitor settings. This was done because it could create unsupported arrangements that triggered a cascade of strange bugs throughout the system. Waking up a sleeping computer by pressing the power button no longer causes the bizarre logout screen to appear after you unlock it, which is a relief. Alignment issues in the settings page for the Digital Clock widget were also resolved. The list of bug fixes for 6.4 is extensive. The development team has fixed the most common crash affecting the System Monitor and squashed another one related to a divide-by-zero error. For users with multiple monitors, a long-awaited fix has landed that prevents windows from disappearing when the screen they are on gets disconnected. Even the humble Sticky Notes widget received attention; it will no longer freeze the Plasma shell if you place it on a very thick panel. Discover, the software center, also had a bug patched that caused it to crash if closed too quickly after launch. Here's the full list of improvements: Putting a Sticky Note widget on a very thick panel can no longer cause Plasma to freeze; now, you can use a thick panel with a sticky note on it as a notes sidebar. Fixed the most common System Monitor crash. Fixed another crash in System Monitor, this time a divide-by-zero. Fixed a case where xdg-desktop-portal-kde could crash after you choose a video source to start streaming. Fixed a bug that caused Discover to crash if you close it immediately after it launched. Fixed multiple subtle bugs with the screen chooser widget and OSD that caused it to do the wrong thing on rotated screens or when mirroring screens. Fixed a bug that caused the System Settings’ search field not to be focused properly when pressing Ctrl+F while any UI elements in a settings page already had focus. Fixed the root cause of multiple issues involving windows disappearing when you disconnect the screen they’re on. Clicking a button on a desktop widget that opens a menu no longer inappropriately makes the widget enter Widget Edit Mode. Files with a # or ? character in their name or full path are no longer unexpectedly missing from the history lists in Kicker/Kickoff/etc launchers. Fixed a bug that caused the screen chooser window to sometimes not appear as expected when OBS was launched. Fixed a bug that caused tiled windows on a multi-screen setup to lose their tiling settings when the system went to sleep and woke up again. The "Move window to [activity]" feature now works properly when invoked from the Task Manager widget. Fixed a bug in the Kicker Application Menu that caused keyboard navigation not to work if the popup opened with an item under the pointer. In the Overview effect’s grid view, dragging windows tiled on one virtual desktop over to a different virtual desktop now keeps them tiled as expected. The focus may be on the imminent 6.4 release, but work on what comes next never really stops. Looking ahead, development on Plasma 6.5 is already well underway. Just last week, the KDE team brought several performance improvements to Plasma 6.5.0. This week, the team fixed a nagging bug that sometimes caused the Networks widget to think a hotspot was still enabled after disconnecting from Wi-Fi. On the features side, the System Settings' Fonts page now prevents you from breaking your system entirely by setting fonts below 4pt. In addition to that, switching time spans in the Info Center’s energy page now features smooth graph animations. Performance-wise, kwriteconfig should also be faster, allowing changes you made to the keyboard layout using the tool to reflect immediately.
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      +FloatingFatMan
      252
    3. 3
      snowy owl
      252
    4. 4
      ATLien_0
      228
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!