0

Hiya,
I am new to java, I have a very fast paced class and I am learning as much as I can as quickly as I can. I have previous experince with C++ but it will not help here. My Question:

WHY WILL MY PROGRAM NOT RUN!!??
*It compiles and creates a class but does nothing.
*
Here is the code:

class Mobilephone { //class name and attributes
public static void main(String []args) {
}
    private int SerialNumber; //serial number of product
    private int ItemNumber; //item # of product
    private double UnitsStock; //# of units in stock
    private double UnitPrice; //Price per unit
    private double InventoryValue; //The dollar value of the inventory in stock
    private String Department; //The department the product belongs to
    private String ProductName; //product name



//constructor

 public Mobilephone (int item, int serial, double units, double price, double value, String product, String Dept) {

    ItemNumber = item;
    SerialNumber = serial;
    UnitsStock = units;
    UnitPrice = price;
    InventoryValue = value;
    ProductName = product;
    Department = Dept;



} //end constructor

///getter and setter methods for Mobilephone


public void setItemNumber (int item) { //setter for item number
    this.ItemNumber = item;
} //end setter item number
public int getItemNumber() { //getter for item number
    return ItemNumber;
} //end getter item number


public void setSerialNumber (int serial) { //setter for serial number
    this.SerialNumber = serial;
}//end setter for serial number
public int getSerialNumber() { //getter for serial number
    return SerialNumber;
}//end getter for serial number


public void setUnitsStock (double units) { //setter for units in stock
    this.UnitsStock = units;
} //end setter units in stock
public double getUnitsStock() { //getter for units in stock
    return UnitsStock;
} //end getter units in stock


public void setUnitPrice (double price) { //setter for unit price
    this.UnitPrice = price;
} //end setter unit price
public double getUnitPrice() { //getter for unit price
    return UnitPrice;
} //end getter for unit price


public void setInventoryValue (double value) { //setter for inventory value
    this.InventoryValue = value;
} //end setter inventory value
public double getInventoryValue() { //getter for Inventory Value
    return InventoryValue;
} //end getter for Inventory Value


public void setProductName (String product) { //setter for product name
    this.ProductName = product;
} //end setter product name
public String getProductName() { //getter for product name
    return ProductName;
} //end getter product name


public void setDepartment (String dept) { //setter for department
    this.Department = dept;
} //end setter Department
public String getDepartment() { //getter for department
    return Department;
} //end getter Department



//calculate individual product inventory value

public double getInventoryStockValue(){
    return UnitsStock * UnitPrice;
}//end calculate individual product inventory value


//calculate total inventory value

public double calculateInventory(){
    return UnitPrice * UnitsStock;
}//end calculate total inventory value

///end getter and setter methods for Mobilephone

} //end class Mobilephone

Chuck that into editor and save as .java. Javac and creates class. Run java and nothing happends...Annoying.

7
Contributors
10
Replies
40
Views
3 Years
Discussion Span
Last Post by radhakrishna.p
0

The public static void main(String[] args) method is where you put what you want your class to do when it is run. Your main method does nothing as there is nothing in the {}, so you are literally telling your class to do nothing.

0

nothing happens to you codes as you try to run the program because you never use the methods in it.

0

try to visit some java tutorials ..or you can just try to study my code. so that you can make your own code..

Edited by mtangpos16: redunduncy already

-1
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package googleproblem;

/**
 *
 * @author mtangpos
 */
public class GoogleProblem {

    public static void main(String[] args) {
        class Mobilephone { //class name and attributes

            private int SerialNumber; //serial number of product
            private int ItemNumber; //item # of product
            private double UnitsStock; //# of units in stock
            private double UnitPrice; //Price per unit
            private double InventoryValue; //The dollar value of the inventory in stock
            private String Department; //The department the product belongs to
            private String ProductName; //product name
//constructor

            public Mobilephone(int item, int serial, double units, double price, double value, String product, String Dept) {
                this.ItemNumber = item;
                this.SerialNumber = serial;
                this.UnitsStock = units;
                this.UnitPrice = price;
                this.InventoryValue = value;
                this.ProductName = product;
                this.Department = Dept;
            } //end constructor
///getter and setter methods for Mobilephone

            public void setItemNumber(int item) { //setter for item number
                this.ItemNumber = item;
            } //end setter item number

            public int getItemNumber() { //getter for item number
                return ItemNumber;
            } //end getter item number

            public void setSerialNumber(int serial) { //setter for serial number
                this.SerialNumber = serial;
            }//end setter for serial number

            public int getSerialNumber() { //getter for serial number
                return SerialNumber;
            }//end getter for serial number

            public void setUnitsStock(double units) { //setter for units in stock
                this.UnitsStock = units;
            } //end setter units in stock

            public double getUnitsStock() { //getter for units in stock
                return UnitsStock;
            } //end getter units in stock

            public void setUnitPrice(double price) { //setter for unit price
                this.UnitPrice = price;
            } //end setter unit price

            public double getUnitPrice() { //getter for unit price
                return UnitPrice;
            } //end getter for unit price

            public void setInventoryValue(double value) { //setter for inventory value
                this.InventoryValue = value;
            } //end setter inventory value

            public double getInventoryValue() { //getter for Inventory Value
                return InventoryValue;
            } //end getter for Inventory Value

            public void setProductName(String product) { //setter for product name
                this.ProductName = product;
            } //end setter product name

            public String getProductName() { //getter for product name
                return ProductName;
            } //end getter product name

            public void setDepartment(String dept) { //setter for department
                this.Department = dept;
            } //end setter Department

            public String getDepartment() { //getter for department
                return Department;
            } //end getter Department
//calculate individual product inventory value

            public double getInventoryStockValue() {
                return UnitsStock * UnitPrice;
            }//end calculate individual product inventory value
//calculate total inventory value

            public double calculateInventory() {
                return UnitPrice * UnitsStock;
            }//end calculate total inventory value
///end getter and setter methods for Mobilephone
        } //end class Mobilephone

        //_____________________________________________________________

        //simple example on how to use the methods 
        //constructor
        Mobilephone mp = new Mobilephone(1, 1, 1, 1000, 1, "iphone", "mobile_department");

        //if you want to calculateInventory
        System.out.println("calculateInventory= " + mp.calculateInventory());

        //if you want to getDepartment();
        System.out.println("getDepartment= " + mp.getDepartment());

        //if you want to getInventoryStockValue();
        System.out.println("getInventoryStockValue= " + mp.getInventoryStockValue());

        //if you want to getInventoryValue();
        System.out.println("getInventoryValue =" + mp.getInventoryValue());

        //if you want to getItemNumber();
        System.out.println("getItemNumber= " + mp.getItemNumber());

        //if you want to getProductName();
        System.out.println("getProductName= " + mp.getProductName());

        //if you want to getSerialNumber();
        System.out.println(" getSerialNumber =" + mp.getSerialNumber());

        //if you want to getUnitPrice()
        System.out.println("getUnitPrice =" + mp.getUnitPrice());

        //if you want to getUnitsStock()
        System.out.println("getUnitsStock = " + mp.getUnitsStock());


        //___________________________
        //if you want to set the values just use the setters method
        //sample
        //mp.setDepartment("blabla");
        //ans so on....

    }
}
-2

hai mtangpos16,

i have done some modifications to your code

you can not write a class inside a main method because main() method is starting point for creating objects but not starting point of the class creation/definition/declaration.

i think your code would be like as follows

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package googleproblem;

/**
 *
 * @author mtangpos
 */
public class GoogleProblem 
{

    public static void main(String[] args) {    

        Mobilephone mp = new Mobilephone(1, 1, 1, 1000, 1, "iphone", "mobile_department");

        //if you want to calculateInventory
        System.out.println("calculateInventory= " + mp.calculateInventory());

        //if you want to getDepartment();
        System.out.println("getDepartment= " + mp.getDepartment());

        //if you want to getInventoryStockValue();
        System.out.println("getInventoryStockValue= " + mp.getInventoryStockValue());

        //if you want to getInventoryValue();
        System.out.println("getInventoryValue =" + mp.getInventoryValue());

        //if you want to getItemNumber();
        System.out.println("getItemNumber= " + mp.getItemNumber());

        //if you want to getProductName();
        System.out.println("getProductName= " + mp.getProductName());

        //if you want to getSerialNumber();
        System.out.println(" getSerialNumber =" + mp.getSerialNumber());

        //if you want to getUnitPrice()
        System.out.println("getUnitPrice =" + mp.getUnitPrice());

        //if you want to getUnitsStock()
        System.out.println("getUnitsStock = " + mp.getUnitsStock());       

    }



class Mobilephone { //class name and attributes

            private int SerialNumber; //serial number of product
            private int ItemNumber; //item # of product
            private double UnitsStock; //# of units in stock
            private double UnitPrice; //Price per unit
            private double InventoryValue; //The dollar value of the inventory in stock
            private String Department; //The department the product belongs to
            private String ProductName; //product name
//constructor

            public Mobilephone(int item, int serial, double units, double price, double value, String product, String Dept) {
                this.ItemNumber = item;
                this.SerialNumber = serial;
                this.UnitsStock = units;
                this.UnitPrice = price;
                this.InventoryValue = value;
                this.ProductName = product;
                this.Department = Dept;
            } //end constructor
///getter and setter methods for Mobilephone

            public void setItemNumber(int item) { //setter for item number
                this.ItemNumber = item;
            } //end setter item number

            public int getItemNumber() { //getter for item number
                return ItemNumber;
            } //end getter item number

            public void setSerialNumber(int serial) { //setter for serial number
                this.SerialNumber = serial;
            }//end setter for serial number

            public int getSerialNumber() { //getter for serial number
                return SerialNumber;
            }//end getter for serial number

            public void setUnitsStock(double units) { //setter for units in stock
                this.UnitsStock = units;
            } //end setter units in stock

            public double getUnitsStock() { //getter for units in stock
                return UnitsStock;
            } //end getter units in stock

            public void setUnitPrice(double price) { //setter for unit price
                this.UnitPrice = price;
            } //end setter unit price

            public double getUnitPrice() { //getter for unit price
                return UnitPrice;
            } //end getter for unit price

            public void setInventoryValue(double value) { //setter for inventory value
                this.InventoryValue = value;
            } //end setter inventory value

            public double getInventoryValue() { //getter for Inventory Value
                return InventoryValue;
            } //end getter for Inventory Value

            public void setProductName(String product) { //setter for product name
                this.ProductName = product;
            } //end setter product name

            public String getProductName() { //getter for product name
                return ProductName;
            } //end getter product name

            public void setDepartment(String dept) { //setter for department
                this.Department = dept;
            } //end setter Department

            public String getDepartment() { //getter for department
                return Department;
            } //end getter Department
//calculate individual product inventory value

            public double getInventoryStockValue() {
                return UnitsStock * UnitPrice;
            }//end calculate individual product inventory value
//calculate total inventory value

            public double calculateInventory() {
                return UnitPrice * UnitsStock;
            }//end calculate total inventory value
///end getter and setter methods for Mobilephone
        } //end class Mobilephone

}

check it once and let me know the status

happy coding

Edited by radhakrishna.p: giving clarity on the solution

0

@OP : bguild points out the core of your problem, unless you solve that , nothings gonna work out..
also , another part you might wish to take notice of is your use of the this keyword.

public void setDepartment (String dept) { //setter for department
    this.Department = dept;

you dont need to use this here , as the name of the argument passed is different to the name of the instance variable. if they were same , this would have been needed, but since thats not the case , its reduntant to use this here.

ps : a personal opinion : comments are powerful , and is a good practice to follow , however too much of a good thing isnt necessarily better. putting comments on almost every line makes the code look cluttered up. keep 'em where it impacts the most.

Edited by somjit{}

0

... just shows why it's a good idea to compile (and even test!) any code before you post it here ... nothing gets past the eagle eyes ... makes you look a bit silly when you tell a beginner "do it like this" where "this" doesn't even compile :)

0

Thanks guys,
I knew posting here was a good Idea. I figured it was beacuse I told the program to do nothing as a program only does what you tell it, I just didn't know how I told it to do nothing. Makes sense now. I do not like java as much as C++ but it is the prefered for many mobile companies. Suppose we should look out for ceylon now.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.