Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Help with assignment

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with assignment

    I'm in my first Java class and I'm having a hard time figuring out why this code isn't working for my assignment. The code here is asking the user to input the name of a bike. inStock will check and see if the bike is in stock. If it isn't, the user is asked for the rest of the bike's info and a new Bike instance is created. Then addNew is called to add the bike I just created into my bike shop.

    System.out.print("Please enter the name of the bike to be added:\n");
    		name = input.next();
     
    if (VinnyBikeShop.inStock(name, 0)) {
    	        System.out.print("The bike is NOT in stock. It will need to be added.\n");
    			System.out.println("What is the size of the bike?");
    			size = input.nextInt();
    			System.out.println("What is the color of the bike?");
    			color = input.next();
    			System.out.println("How many bikes do you want to add?");
    			quantity = input.nextInt();
    			System.out.println("What is the price of the bike?");
    			price = input.nextDouble();
    			aBike = new Bike(name, size, color, price, quantity);
    			VinnyBikeShop.addNew(aBike);
    	        }

    As far as I can tell, it's not adding that bike into the bike shop. There's a listDetails method that lists the name, size, etc. of a bike. I try calling that after adding the bike and it comes back null.

    The code for the Bike constructor is:

    public Bike(String theName, int itsSize, String itsColor, double cost, int qty) {

    I'd really appreciate any insight.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Help with assignment

    VinnyBikeShop.addNew(aBike);
    Is this the call that should add the bike to the bike shop? Have you looked at the code in that method?
    You don't show the code, so we can only imagine what the missing code is doing.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment

    Sorry, should have mentioned that that method is part of a class the professor provided, and we aren't supposed to touch.

    Here's what it looks like:

    public void addNew(Bike b) {
      		if (totalbikes < bikes.length) {
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end if
      		else {
      			Bike[] temp = new Bike[totalbikes];
      			int i;
      			int newlength = 2*bikes.length;
      			for (i=0; i<totalbikes;i++)
      				temp[i] = bikes[i];
      			bikes = new Bike[newlength];
      			for (i=0; i<totalbikes; i++)
      				bikes[i] = temp[i];
      			bikes[totalbikes] = b;
      			totalbikes++;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Help with assignment

    listDetails method that lists the name, size, etc. of a bike. I try calling that after adding the bike and it comes back null.
    You're going to have to post all the code for everything you mention. There's no way to anyone can tell what's happening without seeing ALL the code.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment

    You could have easily said that in your last post.

    public void listDetails() {
      		int i;
      		System.out.println();
      		System.out.println("Name                    Size        Color            Price          Quantity");
      		for (i=0;i<totalbikes;i++) {
      			System.out.print((i+1)+". "+bikes[i].getName());
      			int spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getName()).length() + 2;
      			int spaces = 25 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getSize());
      			System.out.print("           ");
      			System.out.print(bikes[i].getColor());
      			spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getColor()).length() + 2;
      			spaces = 18 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getPrice());
      			System.out.print("             ");
      			System.out.println(bikes[i].getQuantity());
      		}//end for
      		System.out.println();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Help with assignment

    You still have not posted ALL the code for the program.

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment

    public class BikeShop { 
     
      private Bike[] bikes;
      private int totalbikes;
      private double gross;
      private final static int SIZE = 30;
     
      	//Constructor method
      	public BikeShop() {
      		bikes = new Bike[SIZE];
      		totalbikes = 0;
      		gross = 0.0;
      	}//end constructor method
     
      	//addNew - adds a new bike object to the bike shop
      	public void addNew(Bike b) {
      		if (totalbikes < bikes.length) {
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end if
      		else {
      			Bike[] temp = new Bike[totalbikes];
      			int i;
      			int newlength = 2*bikes.length;
      			for (i=0; i<totalbikes;i++)
      				temp[i] = bikes[i];
      			bikes = new Bike[newlength];
      			for (i=0; i<totalbikes; i++)
      				bikes[i] = temp[i];
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end else
      	}//end addNew method
     
      	//addQuantity - adds to the quantity of a bike in stock
      	public void addQuantity(String name, int quantity) {
      		int i;
      		for (i=0; i<totalbikes; i++) {
      			if ((bikes[i].getName()).equals(name)) {
      				bikes[i].addStock(quantity);
      				return;
      			}//end if
      		}//end for
      	}//end addQuantity method
     
      	//inStock - checks to see if a given bike in specified quantity is in stock
      	public boolean inStock(String name, int quantity) {
      		int i;
      		for (i=0; i<totalbikes; i++) {
      			if ((bikes[i].getName()).equals(name)) {
      				if (quantity <= bikes[i].getQuantity())
      					return true;
      				else
      					return false;
      			}//end if
      		}//end for
      		return false;
      	}//end inStock method
     
      	//sell - sells a given bike in specified quantity
      	public boolean sell(String name, int quantity) {
      		boolean retval = inStock(name, quantity);
      		if (retval) {
      			int i;
      			for (i =0; i<totalbikes; i++)
      				if (name.equals(bikes[i].getName())) {
      					gross += bikes[i].sell(quantity);
      					if (bikes[i].getQuantity() == 0) {
      						delete(i);
      					}//end if
      					break;
      				}//end if
              }//end for
      		return retval;
      	}//end sell method
     
      	//listNames - lists the names of all bikes in stock and the quantity on hand
      	public void listNames() {
      		int i;
      		System.out.println();
      		System.out.println("Name                                   Quantity");
      		for (i=0;i<totalbikes;i++) {
      			System.out.print((i+1)+". "+bikes[i].getName());
      			int spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getName()).length() + 2;
      			int spaces = 40 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.println(bikes[i].getQuantity());
      		}//end for
      		System.out.println();
      	}//end listNames method
     
      	//listDetails - prints all information about in stock bikes
      	public void listDetails() {
      		int i;
      		System.out.println();
      		System.out.println("Name                    Size        Color            Price          Quantity");
      		for (i=0;i<totalbikes;i++) {
      			System.out.print((i+1)+". "+bikes[i].getName());
      			int spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getName()).length() + 2;
      			int spaces = 25 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getSize());
      			System.out.print("           ");
      			System.out.print(bikes[i].getColor());
      			spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getColor()).length() + 2;
      			spaces = 18 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getPrice());
      			System.out.print("             ");
      			System.out.println(bikes[i].getQuantity());
      		}//end for
      		System.out.println();
      	}//end listDetails method
     
      	//getIncome - return how much $ the shop has made
      	public double getIncome() {
      		return gross;
      	}

    public class Bike
     
    {
     
    	private String name;
    	private int size;
    	private String color;
    	private double price;
    	private int quantity;
     
    	public Bike(String theName, int itsSize, String itsColor, double cost, int qty) {
    	}
     
     
    	public String getName() {
    		return name;
    	}
     
    	public int getSize() {
    		return size;
    	}
     
    	public String getColor() {
    		return color;
    	}
     
    	public double getPrice() {
    		return price;
    	}
     
    	public int getQuantity() {
    		return quantity;
    	}
     
    	public String toString() {
    		//Not used in program
    	}
     
    	public void reduceStock(int change) {
    		if (quantity > 0 && change <= quantity) 
    			quantity -= change;
    		return;
     
    	}
     
    	public void addStock (int change) {
    		quantity += change;
    		return;
    	}
     
    	public double sell(int num) {
     
    		if (quantity > 0 && num <= quantity) {
    			reduceStock(num);
    		}
    			else
    				System.out.println("Sorry, but there are not enough bikes to sell.");
    		return num*price;
    	}
     
     
     
    }

    import java.util.*;
     
    public class MyBikeShop {
     
    	public static int menucode;
    	public static BikeShop VinnyBikeShop = new BikeShop();
    	public static Bike aBike;
    	public static String name;
    	public static int size;
    	public static String color;
    	public static int quantity;
    	public static int num;
    	public static double price;
     
     
    	public static void main(String[] args) {
     
    	Scanner input = new Scanner(System.in);
    	String lineSeparator = System.getProperty("line.separator");
    	input.useDelimiter(lineSeparator);
     
    	while (menucode != 6) {
    	System.out.println("====================================================================");
    	System.out.println("                         Vinny's Bike Shop");
    	System.out.println("====================================================================");
        System.out.println("1 - Add a bike to the stock");
    	System.out.println("2 - Sell a bike in stock");
    	System.out.println("3 - List the names and sizes of the bikes in stock");
    	System.out.println("4 - List all information about the bikes in stock");
    	System.out.println("5 - Print out the total amount of sales of the bike shop");
    	System.out.println("6 - Quit");
    	System.out.print("\nPlease Select an Option: \n");
     
    	menucode = input.nextInt();
     
     
    	if (menucode == 1) {
     
     
    		System.out.print("Please enter the name of the bike to be added:\n");
    		name = input.next();
     
    		if (VinnyBikeShop.inStock(name, 0)) {
    	        System.out.print("The bike is NOT in stock. It will need to be added.\n");
    			System.out.println("What is the size of the bike?");
    			size = input.nextInt();
    			System.out.println("What is the color of the bike?");
    			color = input.next();
    			System.out.println("How many bikes do you want to add?");
    			quantity = input.nextInt();
    			System.out.println("What is the price of the bike?");
    			price = input.nextDouble();
    			aBike = new Bike(name, size, color, price, quantity);
    			VinnyBikeShop.addNew(aBike);
    	        }
     
                            if (VinnyBikeShop.inStock(name, quantity)) { 
    			System.out.println("That bike is in stock");
    			System.out.println("How many would you like to sell?");
                           }
     
    	}
     
    	else if (menucode == 2) 
    		//Menu choice for selling a bike.
     
    	else if (menucode == 3) 
    		VinnyBikeShop.listNames();
     
    	else if (menucode == 4) 
    		VinnyBikeShop.listDetails();
     
    	else if (menucode == 5) 
    		VinnyBikeShop.getIncome();
     
    	}	
     
    	System.exit(0);
     
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Help with assignment

    Look at these lines, the code and output message do not agree:
     
    		if (VinnyBikeShop.inStock(name, 0)) {
    	        System.out.print("The bike is NOT in stock. It will need to be added.\n");

    Comment on other things:
    Always use {} with nested if .. else if ...
    Variables names should start with lower case letters.
    Do NOT use static variables unless ABSOLUTELY necessary.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Help with assignment

    Quote Originally Posted by NPVinny View Post
    public class BikeShop { 
     
      private Bike[] bikes;
      private int totalbikes;
      private double gross;
      private final static int SIZE = 30;
     
      	//Constructor method
      	public BikeShop() {
      		bikes = new Bike[SIZE];
      		totalbikes = 0;
      		gross = 0.0;
      	}//end constructor method
     
      	//addNew - adds a new bike object to the bike shop
      	public void addNew(Bike b) {
      		if (totalbikes < bikes.length) {
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end if
      		else {
      			Bike[] temp = new Bike[totalbikes];
      			int i;
      			int newlength = 2*bikes.length;
      			for (i=0; i<totalbikes;i++)
      				temp[i] = bikes[i];
      			bikes = new Bike[newlength];
      			for (i=0; i<totalbikes; i++)
      				bikes[i] = temp[i];
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end else
      	}//end addNew method
     
      	//addQuantity - adds to the quantity of a bike in stock
      	public void addQuantity(String name, int quantity) {
      		int i;
      		for (i=0; i<totalbikes; i++) {
      			if ((bikes[i].getName()).equals(name)) {
      				bikes[i].addStock(quantity);
      				return;
      			}//end if
      		}//end for
      	}//end addQuantity method
     
      	//inStock - checks to see if a given bike in specified quantity is in stock
      	public boolean inStock(String name, int quantity) {
      		int i;
      		for (i=0; i<totalbikes; i++) {
      			if ((bikes[i].getName()).equals(name)) {
      				if (quantity <= bikes[i].getQuantity())
      					return true;
      				else
      					return false;
      			}//end if
      		}//end for
      		return false;
      	}//end inStock method
     
      	//sell - sells a given bike in specified quantity
      	public boolean sell(String name, int quantity) {
      		boolean retval = inStock(name, quantity);
      		if (retval) {
      			int i;
      			for (i =0; i<totalbikes; i++)
      				if (name.equals(bikes[i].getName())) {
      					gross += bikes[i].sell(quantity);
      					if (bikes[i].getQuantity() == 0) {
      						delete(i);
      					}//end if
      					break;
      				}//end if
              }//end for
      		return retval;
      	}//end sell method
     
      	//listNames - lists the names of all bikes in stock and the quantity on hand
      	public void listNames() {
      		int i;
      		System.out.println();
      		System.out.println("Name                                   Quantity");
      		for (i=0;i<totalbikes;i++) {
      			System.out.print((i+1)+". "+bikes[i].getName());
      			int spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getName()).length() + 2;
      			int spaces = 40 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.println(bikes[i].getQuantity());
      		}//end for
      		System.out.println();
      	}//end listNames method
     
      	//listDetails - prints all information about in stock bikes
      	public void listDetails() {
      		int i;
      		System.out.println();
      		System.out.println("Name                    Size        Color            Price          Quantity");
      		for (i=0;i<totalbikes;i++) {
      			System.out.print((i+1)+". "+bikes[i].getName());
      			int spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getName()).length() + 2;
      			int spaces = 25 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getSize());
      			System.out.print("           ");
      			System.out.print(bikes[i].getColor());
      			spaceused = (int)(Math.floor(Math.log(i+1)/Math.log(10)))+1;
      			spaceused += (bikes[i].getColor()).length() + 2;
      			spaces = 18 - spaceused;
      			if (spaces <= 0) spaces = 1;
      			for (int j=0;j<spaces;j++)
      				System.out.print(" ");
      			System.out.print(bikes[i].getPrice());
      			System.out.print("             ");
      			System.out.println(bikes[i].getQuantity());
      		}//end for
      		System.out.println();
      	}//end listDetails method
     
      	//getIncome - return how much $ the shop has made
      	public double getIncome() {
      		return gross;
      	}

    public class Bike
     
    {
     
    	private String name;
    	private int size;
    	private String color;
    	private double price;
    	private int quantity;
     
    	public Bike(String theName, int itsSize, String itsColor, double cost, int qty) {
    	}
     
     
    	public String getName() {
    		return name;
    	}
     
    	public int getSize() {
    		return size;
    	}
     
    	public String getColor() {
    		return color;
    	}
     
    	public double getPrice() {
    		return price;
    	}
     
    	public int getQuantity() {
    		return quantity;
    	}
     
    	public String toString() {
    		//Not used in program
    	}
     
    	public void reduceStock(int change) {
    		if (quantity > 0 && change <= quantity) 
    			quantity -= change;
    		return;
     
    	}
     
    	public void addStock (int change) {
    		quantity += change;
    		return;
    	}
     
    	public double sell(int num) {
     
    		if (quantity > 0 && num <= quantity) {
    			reduceStock(num);
    		}
    			else
    				System.out.println("Sorry, but there are not enough bikes to sell.");
    		return num*price;
    	}
     
     
     
    }

    import java.util.*;
     
    public class MyBikeShop {
     
    	public static int menucode;
    	public static BikeShop VinnyBikeShop = new BikeShop();
    	public static Bike aBike;
    	public static String name;
    	public static int size;
    	public static String color;
    	public static int quantity;
    	public static int num;
    	public static double price;
     
     
    	public static void main(String[] args) {
     
    	Scanner input = new Scanner(System.in);
    	String lineSeparator = System.getProperty("line.separator");
    	input.useDelimiter(lineSeparator);
     
    	while (menucode != 6) {
    	System.out.println("====================================================================");
    	System.out.println("                         Vinny's Bike Shop");
    	System.out.println("====================================================================");   
        System.out.println("1 - Add a bike to the stock");
    	System.out.println("2 - Sell a bike in stock");
    	System.out.println("3 - List the names and sizes of the bikes in stock");
    	System.out.println("4 - List all information about the bikes in stock");
    	System.out.println("5 - Print out the total amount of sales of the bike shop");
    	System.out.println("6 - Quit");
    	System.out.print("\nPlease Select an Option: \n");
     
    	menucode = input.nextInt();
     
     
    	if (menucode == 1) {
     
     
    		System.out.print("Please enter the name of the bike to be added:\n");
    		name = input.next();
     
    		if (VinnyBikeShop.inStock(name, 0)) {
    	        System.out.print("The bike is NOT in stock. It will need to be added.\n");
    			System.out.println("What is the size of the bike?");
    			size = input.nextInt();
    			System.out.println("What is the color of the bike?");
    			color = input.next();
    			System.out.println("How many bikes do you want to add?");
    			quantity = input.nextInt();
    			System.out.println("What is the price of the bike?");
    			price = input.nextDouble();
    			aBike = new Bike(name, size, color, price, quantity);
    			VinnyBikeShop.addNew(aBike);
    	        }
     
                            if (VinnyBikeShop.inStock(name, quantity)) { 
    			System.out.println("That bike is in stock");
    			System.out.println("How many would you like to sell?");
                           }
     
    	}
     
    	else if (menucode == 2) 
    		//Menu choice for selling a bike.
     
    	else if (menucode == 3) 
    		VinnyBikeShop.listNames();
     
    	else if (menucode == 4) 
    		VinnyBikeShop.listDetails();
     
    	else if (menucode == 5) 
    		VinnyBikeShop.getIncome();
     
    	}	
     
    	System.exit(0);
     
    	}
     
    }

    Here might be one source of error:

    Scanner input = new Scanner(System.in);

    Maybe I'm wrong, but normally every time I've done that Scanner thing, I've done made it static.
    Also, I normally put that Scanner declaration before the main method.

    Also, I think input has to be static because the main method is static. However, I do think you went overboard with the statics.

    Also, I don't think the compiler likes it if you user a non-static variable from a static context or maybe it was the a static variable from a non-static context.

    In any case, since the Bikes array bikes is of length SIZE, why not put that instead of bikes.length to shorten the code a little bit?

    Also, until you initialize the values you're passing as parameters, your constructor won't like them and will likely throw some error there saying that each of the parameters might not have been initialized and your get methods will likely return null, if not throw a NullPointerException.

    Also, in case your wondering about Exceptions, here's a common one

    try
    { // try block looks for exception and goes to catch if it throws it.  Your own exception classes must be thrown in the try block with the words throw and new yourExceptionClass
     
    int i;
     
    i = input.nextInt();
    }
     
    catch)InputMismatchException imeRef)
    { // catch handles exception.  In this case, this one handles it if you have the wrong input type, i.e., entering 3.14 or Bob for an integer value
     
    System.out.println("Exception " 
                                 + imeRef.toString());
    } 
    catch (ArithmeticException aeRef)
    { // this one handles if you do something like divide by zero
     System.out.println("Exception" + aeRef.toString());
    }

    You probably don't need to worry about exceptions yet, but just something to think about.



    And what does += mean?
    spaceused += (bikes[i].getName()).length() + 2;

    Also, doesn't menuCode have to be initialized to some value?

    In fact, don't all of your variables have to be initialized?



    	//addNew - adds a new bike object to the bike shop
      	public void addNew(Bike b) {
      		if (totalbikes < bikes.length) {
      			bikes[totalbikes] = b;
      			totalbikes++;
      		}//end if
    Couldn't you make a new bike object by doing this:
    Bike b = new Bilke(Bike parameters);(
    However, you have totalbikes = 0 in constructor and never changed that before if...else loop begins so else part should never happen as it is now. However, if you want totalbikes to be the number of bikes you have, a.k.a., quantity, then why not do this:
    Bike b = new Bike(Bike Parameters);
    totalbikes = b.getQuantity();



    Also, it, though it could be because I'm very tired right now, almost looks like you're telling it to set it to the Bike array to totalbikes. However, as you can keep adding, you're array should get larger. But once array Bike[] temp can't keep being enlarged, at least that's what I heard, that you can't just make an existing array larger, you have to make a whole new array. That's why they invented vectors.

    For vectors, it's

    Vector<Integer>  vectorNameOne= new Vector<Integer>;
    Vector <String> vectorNameTwo = new Vector<String>;
    Vector <Double> vectorNameThree = new Vector<Double>;
    Vector<Long> vectorNameFour = new Vector<Long>;
    Vector<Float> vectorNameFive = new Vector<Float>;
    Vector<Boolean> vectorNameSix = new Vector<Boolean>; // why anyone would have a vector of booleans I don't know, but there could be cases where they might have to I guess.
    Vector<Char> vectorNameSeven = new Vector<Char>;
    Vector <Short> vectorNameEight = new Vector<Short>;
    Vector<someClass> vectorNameNine= new Vector<someClass>;
    Vector<Bike> bikeVector = new Vector<Bike>;

    http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html#copyInto%28java.lang.Object[]%29

    I'm getting rather tired right now.
    I'll come back tomorrow.
    Last edited by helloworld922; July 3rd, 2010 at 09:18 AM.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with assignment

    please use [code] tags, or even better, [highlight="Java"] tags

    // why anyone would have a vector of booleans I don't know, but there could be cases where they might have to I guess.
    A 1-D vector of booleans is extremely useful for implementing the sieve of eratosthenes (finding prime numbers), though personally I would either use a boolean array or an ArrayList of booleans, even if I decided to use multi-threaded approach to implementing the sieve (I rarely use vectors in Java).

    There is also nothing wrong with declaring a Scanner object inside a method (I usually do this).

    What I suspect is the problem is that your bike constructor does not actually set anything.

    public Bike(String theName, int itsSize, String itsColor, double cost, int qty) {
        // need to actually set the object variables in here
        this.name = theName;
        this.size = itsSize;
        this.color = itsColor;
        this.price = cost;
        this.quantity = qty;
    }

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    NPVinny (July 3rd, 2010)

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help with assignment

    Ok, maybe nothing wrong with having it inside the main method I suppose. However, main is static. Shouldn't Scanner be static too?

    Also, when I was suggesting Vectors, it was because he had something like:
    if (totalbikes < bike.length)
    {
    something
    }
     
    else
    {
    Bike[] temp = new  Bike[totalbikes];
    }
    or something like that.

    I heard that you can't just increase the size of the array.
    Last edited by helloworld922; July 3rd, 2010 at 02:08 PM. Reason: please use [highlight=Java][/highlight] around java code

  13. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with assignment

    It's preference really in that case. There's nothing wrong with declaring it inside the main method because then it will be local to the main method.

    It's true you can't just "increase" the size of an existing array. You must allocate a new bigger array, then copy over all the old data. Indeed, this is what the underlying implementations of Vector and ArrayList do (of course, they increase the capacity by more than 1 so you don't need to keep re-allocating and copying every time a new item is added to them).

    // A sample of how a simple Vector class could be implemented
    public class MyVector<Type>
    {
         private Type[] data;
         private int length;
     
        public MyVector()
        {
            // initial capacity: 10
            data = new Type[10];
            length = 0;
        }
     
        public Type get(int index)
        {
            if (index < 0 || index >= length)
            {
                throw new ArrayIndexOutOfBoundsException(index);
            }
            return data[index];
        }
     
        public void add(Type item)
        {
            if(length >= Capacity)
            {
                // need to increase the array size
                Type[] temp = new Type[data.length * 2];
                // copy over old data
                for(int i = 0; i < data.length; i++)
                {
                    temp[i] = data[i];
                }
                data = temp;
            }
            data[length] = item;
            length++;
        }
     
        public int size()
        {
            return length;
        }
     
        public Type remove(int index)
        {
            Type item = get(index);
            for(int i = index+1; i < length; i++)
            {
                data[i-1] = data[i];
            }
            length--;
            return type;
        }
    }

  14. #13
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment

    Quote Originally Posted by helloworld922 View Post
    please use [code] tags, or even better, [highlight="Java"] tags



    A 1-D vector of booleans is extremely useful for implementing the sieve of eratosthenes (finding prime numbers), though personally I would either use a boolean array or an ArrayList of booleans, even if I decided to use multi-threaded approach to implementing the sieve (I rarely use vectors in Java).

    There is also nothing wrong with declaring a Scanner object inside a method (I usually do this).

    What I suspect is the problem is that your bike constructor does not actually set anything.

    public Bike(String theName, int itsSize, String itsColor, double cost, int qty) {
        // need to actually set the object variables in here
        this.name = theName;
        this.size = itsSize;
        this.color = itsColor;
        this.price = cost;
        this.quantity = qty;
    }
    Yep. This was the problem. Thanks!

Similar Threads

  1. College Assignment please help
    By The Lost Plot in forum Collections and Generics
    Replies: 7
    Last Post: March 13th, 2012, 09:27 AM
  2. please help me in my assignment :(
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2010, 07:59 AM
  3. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 03:20 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 08:44 PM