Results 1 to 5 of 5

Thread: [RESOLVED] Beginner Homework issue

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Beginner Homework issue

    Hi,

    I have only been learning Java as part of my software development course for 3 weeks. I have to hand an exercise I have to hand it in by next week except I don't understand the errors I'm receiving.

    This is my code:

    Java main:

    Code:
    public class NewMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
                   Unit unit;
                    unit = new Unit();
                    unit.setmaintainenceCost(100);
                    unit.getrentAmount(60);
                    unit.setnumRooms(1);
                    unit.setunitCost("Hundred");
                            
            System.out.println("Rent amount: = " + unit.getunitCost());
            System.out.println("Room number: = " + unit.getnumRooms(numRooms));
                   System.out.println("maintainenceCost: = " + unit.getmaintainenceCost());
            System.out.println("unitCost = " + unit.getunitCost(null));
        }
    
    }
    Class:

    Code:
    public class Unit {
        //constants
    
        public static final String DEFAULT_UNIT_COST = "DEFAULT_UNIT_COST";
        public static final int DEFAULT_NUM_ROOMS= 0;
        public static final int DEFAULT_RENT_AMOUNT= 0;
        public static final int DEFAULT_MAINTAINENCE_COST= 0;
    
        //instance variables
        private int rentAmount;
        private int numRooms;
        private int maintainenceCost;
        private String unitCost;
    
        //no arg constructor
        public Unit() {
    
        }
    
        // all arg constructor
        public Unit(int rentAmount, int numRooms, int maintainenceCost, String unitCost) {
            this.rentAmount = rentAmount;
            this.numRooms =  numRooms;
            this.maintainenceCost = maintainenceCost;
            this.unitCost = unitCost;
        }
    
        //getter for numRooms
        public int getnumRooms(int numRooms) {
            return this.numRooms;
        }
        //setter for numRooms
    
        public void setnumRooms(int numRooms) {
            this.numRooms = numRooms;
        }
    
            //getter for rentAmount
        public int getrentAmount(int rentAmount) {
            return this.rentAmount;
        }
        //setter for rentAmount
    
        public void setrentAmount(int rentAmount) {
            this.rentAmount = rentAmount;
        }
              //getter for maintainenceCost
        public int getmaintainenceCost(int maintainenceCost) {
            return this.maintainenceCost;
        }
        //setter for maintainenceCost
    
        public void setmaintainenceCost(int maintainenceCost) {
            this.maintainenceCost = maintainenceCost;
        }
            public String getunitCost(String unitCost) {
            return this.unitCost;
        }
        //setter for maintainenceCost
    
        public void setunitCost(String unitCost) {
            this.unitCost = unitCost;
        }
        //toString method used for debugging
        public String toString() {
            return super.toString() +
                    "[" +
                    "\n rentAmount = " + this.rentAmount  +
                    "\n numRooms = " + this.numRooms +
                    "\n maintainenceCost = " + this.maintainenceCost +
                    "\n unitCost = " + this.unitCost.toString() +
                    "]";
        }
    }
    Thanks,



    Nightwalker
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Beginner Homework issue

    Just looking at the code here I notice a few things.

    #1 You're not passing anything into your function "getunitCost," which you've written to require a string parameter.
    #2 You're using the variable "numRooms" and you've never declared it anywhere nor have you assigned it anything.
    #3 "getmaintainenceCost" requires an integer parameter, which you haven't given.

    If you look at the unit class, it defines what you need to pass and how you need to handle everything. You don't seem to be following it in your "NewMain" class.

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Beginner Homework issue

    Using the follow code it works!

    Java main:

    Code:
    public class NewMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
                   Unit unit;
                    unit = new Unit();
                    unit.setmaintainenceCost(100);
                    unit.setrentAmount(6);
                    unit.setnumRooms(1);
                    unit.setunitCost("Hundred");
                            
            System.out.println("Rent amount: = " + unit.getunitCost(null));
            System.out.println("Room number: = " + unit.getnumRooms(1));
                   System.out.println("maintainenceCost: = " + unit.getmaintainenceCost(1));
            System.out.println("unitCost = " + unit.getunitCost(null));
        }
    
    }
    Class:

    Code:
    public class Unit {
        //constants
    
        public static final String DEFAULT_UNIT_COST = "DEFAULT_UNIT_COST";
        public static final int DEFAULT_NUM_ROOMS= 0;
        public static final int DEFAULT_RENT_AMOUNT= 0;
        public static final int DEFAULT_MAINTAINENCE_COST= 0;
    
        //instance variables
        private int rentAmount;
        private int numRooms;
        private int maintainenceCost;
        private String unitCost;
    
        //no arg constructor
        public Unit() {
    
        }
    
        // all arg constructor
        public Unit(int rentAmount, int numRooms, int maintainenceCost, String unitCost) {
            this.rentAmount = rentAmount;
            this.numRooms =  numRooms;
            this.maintainenceCost = maintainenceCost;
            this.unitCost = unitCost;
        }
    
        //getter for numRooms
        public int getnumRooms(int numRooms) {
            return this.numRooms;
        }
        //setter for numRooms
    
        public void setnumRooms(int numRooms) {
            this.numRooms = numRooms;
        }
    
            //getter for rentAmount
        public int getrentAmount(int rentAmount) {
            return this.rentAmount;
        }
        //setter for rentAmount
    
        public void setrentAmount(int rentAmount) {
            this.rentAmount = rentAmount;
        }
              //getter for maintainenceCost
        public int getmaintainenceCost(int maintainenceCost) {
            return this.maintainenceCost;
        }
        //setter for maintainenceCost
    
        public void setmaintainenceCost(int maintainenceCost) {
            this.maintainenceCost = maintainenceCost;
        }
            public String getunitCost(String unitCost) {
            return this.unitCost;
        }
        //setter for maintainenceCost
    
        public void setunitCost(String unitCost) {
            this.unitCost = unitCost;
        }
        //toString method used for debugging
        public String toString() {
            return super.toString() +
                    "[" +
                    "\n rentAmount = " + this.rentAmount  +
                    "\n numRooms = " + this.numRooms +
                    "\n maintainenceCost = " + this.maintainenceCost +
                    "\n unitCost = " + this.unitCost.toString() +
                    "]";
        }
    }
    Although, I don't know why I am receiving "Rent amount: = Hundred" not matter what value I set Rent amount to. Also, Rent amount is suppose to be an int not string. I'm going to have to compare my code with my class mates code and see if we both get the same answer.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Beginner Homework issue

    Because you're calling unitCost, not rentAmount.

    Code:
    "Rent amount: = " + unit.getunitCost(null)
    Will always equal what you've assigned it, which you do here:

    Code:
    unit.setunitCost("Hundred");
    I assume what you want, is to call "getrentAmount()." Mind you, I don't know why you're passing it an integer parameter that you never use.

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Beginner Homework issue

    I replaced that line with:

    Code:
    System.out.println("Rent amount: = " + unit.getrentAmount(1));
    and it works.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width