Hello All,
I am working on a project for class and I have everything work except for one little thing. I can compile there is no issues with running the program. However, when get prints the information I get a null as the value for the Mobile OS. I do not expect anyone to do my homework for me, just seeking advice. To bring you up to speed, this week we had to extend a class and add an additional feature to that class. I will post my code below for visualization of what I am working towards.

Orginally it wasn't printing anything, becuase I had it set to printf. I changed it to print then I started to see null. I have looked through this over and over I just don't see where I am going wrong.

import java.util.Scanner;

class Mobile {
    private String name;
    private int number;
    private int quantity;
    private double cost;

    public Mobile(String name, int number, int quantity, double cost) {
        this.name = name;
        this.number = number;
        this.quantity = quantity;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getValue() {
        return cost * quantity;
    }

}

class SmartPhone extends Mobile {
    private String mobileOS;


    public SmartPhone(String name, int number, int quantity, double cost,
            String mobileOS) {
        super(name, number, quantity, cost);
    }
        @Override
    public double getValue()
        {
            return super.getValue()+ getRestockFee();
        }
    public double getRestockFee() {
        return getQuantity() * getCost() * 0.05;
    }

    public String getMobileOS() {
        return mobileOS;
    }

    public void setMobileOS(String mobileOS) {
        this.mobileOS = mobileOS;
    }
}

public class MobilePhone {
    public static void main(String args[]) {

        String itemName;
        String mobileOS;
        int itemNum;
        int itemQuan;
        double unitCost;

        SmartPhone mobileValues[] = new SmartPhone[5];

        for (int i = 0; i < 5; i++) {
            Scanner input = new Scanner(System.in);

            System.out.print("Enter Item Name: ");
            itemName = input.nextLine();

            System.out.print("Enter Mobile OS: ");
            mobileOS = input.nextLine();

            System.out.print("Enter Item Number: ");
            itemNum = input.nextInt();

            System.out.print("Enter Quantity of Item: ");
            itemQuan = input.nextInt();

            System.out.print("Enter Price of Single Unit: ");
            unitCost = input.nextDouble();

            System.out.println();

            mobileValues[i] = new SmartPhone(itemName, itemNum, itemQuan, unitCost, mobileOS);

        }
        for (int i = mobileValues.length - 1; i > 0; i--)// begin of bubble sort
                                                            // for array
        {
            for (int j = 0; j < i; j++) {
                if (mobileValues[j].getName().compareToIgnoreCase(
                        mobileValues[j + 1].getName()) > -1) {
                    SmartPhone temp = mobileValues[j];
                    mobileValues[j] = mobileValues[j + 1];
                    mobileValues[j + 1] = temp;
                }
            }
        }// end of bubble sort

        double overAll = 0;// begin find overall value
        for (int i = 0; i < 5; i++) {
            overAll = overAll + mobileValues[i].getValue();
        }// end of algorthim for finding overall value

        for (int i = 0; i < 5; i++) {
            SmartPhone m = mobileValues[i];
            System.out.println();
            System.out.println("Electronics Department");
            System.out.println("Product: " + m.getName());
                        System.out.println("Mobile OS: "+ m.getMobileOS());
            System.out.println("Item Number: " + m.getNumber());
            System.out.println("Quantity: " + m.getQuantity());
            System.out.printf("Unit Cost: $%.2f\n", m.getCost());
                        System.out.printf("Restock Fee: $%.2f\n", m.getRestockFee());
            System.out.printf("Inventory Value: $%.2f\n", m.getValue());
            System.out.printf("Overall Inventory Value: $%.2f\n", overAll);

        }
    }
}

In your class SmartPhone, you pass the mobileOS to the constructor but you never set it to your field so inside the constructor you should add:
this.mobileOS = mobileOS;

Thank you Slavi, I did as you said and worked perfectly! This isn't relevent to this project, but if I wanted my program to print one item at a time. I thought it would be a nice feature to have when display information from arrays. Would I be able to do it by by just pressing enter, or would I have to use if else and have the user type the word next?

Java is to JavaScript as Car is to Carpet.

"Java is to JavaScript as Car is to Carpet."

Myself, I would reverse the nouns, as in

"Javascript is to Java as Carpet is to Car." :-)

That works to, Ive always heard it the other way! :-)

You could print any item that you desire from the array simply by supplying an array index. Perhaps you could add a field that is say for example phone owner, which is a String and represents the name of the owner. When the program is ran, you could print only the owner names to the user such as:
Press the number of whom's information you would like to see
1. Matthew
2. Slavi
3 ...
4 ....

And then the user is expected to enter a number and then pass that number decremented by 1 (Because arrays are 0 based) to the array and print the object's fields.

An improvement to your program would be to create a method print(), so that you just call that method to print someone's information instead of calling print statements all over the code. In your case you print it only once but in general that's a good practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.