• 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

    • I've been using Windows 11 since the day it was released. No complaints whatsoever from me but then again, I always like trying new things. It's been ROCK SOLID running my Dell XPS 8940 desktop.
    • So many things to unpack here I don't even know where to begin...
    • Cjam 1.9.9.0 by Razvan Serea Cjam is a lightweight and fast MP3 editor for Windows that lets you cut, join, and edit MP3 files without re-encoding. This means your audio quality remains untouched, and edits happen instantly. Cjam is ideal for quick, lossless edits—whether you're trimming music, combining tracks, or preparing audio for learning tools or podcasts. It features batch processing, scripting support, cue and playlist file handling, and a simple interface. Cjam is perfect for anyone who needs efficient MP3 editing without the complexity of full audio suites. Cjam requires a PC running Windows 10 or later and Microsoft .NET 6.0 or later. Key features for Cjam: No Re-encoding: Edit MP3 files without losing quality. Cut and Join MP3: Easily cut, trim, and combine MP3 tracks. Batch Processing: Edit multiple files at once for faster workflows. Scriptable Interface: Automate tasks with a custom command language. Cue and Playlist Support: Handle CUE and playlist files for seamless audio management. Fast and Lightweight: Quick processing with minimal system resources. Lossless Audio Editing: Ensure your edits don't affect audio quality. Simple User Interface: Clean, intuitive design for easy navigation. File Format Support: Works with MP3, Cjam-specific file formats (CJAMC, CJAMJ, CJAM). Cjam 1.9.9.0 fixes: Fixed a bug related to playback display on the main screen Download: Cjam 1.9.9.0 | 1.3 MB (Freeware) Links: Cjam Home Page | Cjam Manual | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • "Users may experience repeated KERNEL_SECURITY_CHECK_FAILURE bugchecks after upgrading to this build." This "showstopper" bug/issue is still present for Dev 26200.5651, so NOT QUITE the June 2025 Snapshot for 26200 release but I think we are close to that release so should be within the next couple releases!
    • ah the "quest for more money" continues 🤣
  • Recent Achievements

    • One Month Later
      jezzzy earned a badge
      One Month Later
    • First Post
      CSpera earned a badge
      First Post
    • One Month Later
      MIR JOHNNY BLAZE earned a badge
      One Month Later
    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      635
    2. 2
      ATLien_0
      281
    3. 3
      +FloatingFatMan
      182
    4. 4
      Michael Scrip
      151
    5. 5
      Steven P.
      117
  • Tell a friend

    Love Neowin? Tell a friend!