Compare cert salaries and plan your next career move
if (object == null) { //do something as the object doesn't exist. } else { //it does exist....now what? }
wastedtime wrote: » I don't see any source code?
try { myNullVariable++; } catch(NullPointerException) { //Code to deal with the situation where the variable is null. }
westward wrote: » CEach time I call the method, it just generates Invoice "1". I know I need to move the declaration "invoiceNumber = 0" to outside the class so it is only called once, but where can it go so it is only called once, and will increment from there?
public void myMethod(){ int invoiceNumber = 0; /* Do something here */ invoiceNumber++; }
package task1; public class Invoice { // Initiate the number of invoices public static int numberOfInvoices = 0; // Initiate private variables private String companyName; private double amountDue; private String chargeDate; private int invoiceNumber = ++numberOfInvoices; // Declare a class object Invoice public Invoice(String companyName, double amountDue, String chargeDate){ // Declare variables setCompanyName(companyName); setAmountDue(amountDue); setChargeDate(chargeDate); } public void setCompanyName(String coName) { companyName = coName; } public void setAmountDue(double Amount) { amountDue = Amount; } public void setChargeDate(String dateCharged) { chargeDate = dateCharged; } public String getCompanyName() { return companyName; } public double getAmountDue() { return amountDue; } public String getChargeDate() { return chargeDate; } public int getInvoiceNumber() { return invoiceNumber; } // Retuns the number of invoices generated so far public static int getNumberOfInvoices() { return numberOfInvoices; } }
package task1; public class InvoiceTester { public static void main(String[] args) { // CREATE FIRST INVOICE Invoice Tester01 = new Invoice("Amazing Software", 5000.00, "January 18, 2009"); // Print this invoices using our methods and verify all values System.out.println("Return data from Tester01 Invoice:"); System.out.println("Company Name is: " + Tester01.getCompanyName()); System.out.println("Invoice amount is: " + Tester01.getAmountDue()); System.out.println("Date of charge is: " + Tester01.getChargeDate()); System.out.println("Invoice number is: " + Tester01.getInvoiceNumber()); // CREATES SECOND INVOICE Invoice Tester02 = new Invoice("Best Programs", 4000.00, "February 18, 2009"); // Print this invoices using our methods and verify all values System.out.println("Return data from Tester02 Invoice:"); System.out.println("Company Name is: " + Tester02.getCompanyName()); System.out.println("Invoice amount is: " + Tester02.getAmountDue()); System.out.println("Date of charge is: " + Tester02.getChargeDate()); System.out.println("Invoice number is: " + Tester02.getInvoiceNumber()); // CREATES THIRD INVOICE Invoice Tester03 = new Invoice("Champion Code", 3000.00, "March 18, 2009"); // Print this invoices using our methods and verify all values System.out.println("Return data from Tester03 Invoice:"); System.out.println("Company Name is: " + Tester03.getCompanyName()); System.out.println("Invoice amount is: " + Tester03.getAmountDue()); System.out.println("Date of charge is: " + Tester03.getChargeDate()); System.out.println("Invoice number is: " + Tester03.getInvoiceNumber()); // Show the total number of invoices generated so far System.out.println("The total number of invoices is: " + Tester03.getNumberOfInvoices()); } }
Compare salaries for top cybersecurity certifications. Free download for TechExams community.