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 8 of 8

Thread: Having a trouble Making a program

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having a trouble Making a program

    Hello everyone

    i came here seeking help on doing some program about bicycles for the university..
    and asked

    Write a TestBikes Main Class that will declare array of bikes of type Bicycle of size 6. Fill it with 6 objetcs of different types that the user will decide. Allow the user to speed up or slow down the speed of any object of his/her choice. Allow the user to get or set any attribute of any object of his/her choice. The attributes of these objects are entered by the user. Then, print the description of all bikes.

    i made all the classes, i just don't know how to make the main method properly, here is my code:

     
    import java.util.*;
     
    abstract public class Bicycle {
     
     
     
        protected int startCadence;
        protected int startSpeed;
        protected int startGear;
     
        public Bicycle (int startCadence,int startSpeed, int startGear){
            this.startCadence = startCadence;
            this.startSpeed = startSpeed;
            this.startGear = startGear;
        }
     
        public int getStartCadence() {
            return startCadence;
        }
     
        public void setStartCadence(int startCadence) {
            this.startCadence = startCadence;
        }
     
        public int getStartGear() {
            return startGear;
        }
     
        public void setStartGear(int startGear) {
            this.startGear = startGear;
        }
     
        public int getStartSpeed() {
            return startSpeed;
        }
     
        public void setStartSpeed(int startSpeed) {
            this.startSpeed = startSpeed;
        }
     
           public void applyBrake(int slower){
    //will decrement the speed by slower
           }
     
           public void speedUp(int faster){
    //) will increment the speed by faster
           }
           public void printDescription(){
               System.out.println("Bike is in gear this.gear with a caence of this.cadence and travelling at a speed of this.speed");
           }
           }

    public class RoadBike extends Bicycle{
     
        private int tireWidth;
     
        public RoadBike(int tireWidth, int startCadence, int startSpeed, int startGear) {
            super(startCadence, startSpeed, startGear);
            this.tireWidth = tireWidth;
        }
     
        public int getTireWidth() {
            return tireWidth;
        }
     
        public void setTireWidth(int tireWidth) {
            this.tireWidth = tireWidth;
        }
     
    public void printDescription(){
     
    }
    }

     
    public class MountainBike extends RoadBike{
     
        private String suspension;
        private int seatHeight;
     
        public MountainBike(String suspension, int seatHeight, int tireWidth, int startCadence, int startSpeed, int startGear) {
            super(tireWidth, startCadence, startSpeed, startGear);
            this.suspension = suspension;
            this.seatHeight = seatHeight;
        }
     
        public int getHeight() {
            return seatHeight;
        }
     
        public void setHeight(int seatHeight) {
            this.seatHeight = seatHeight;
        }
     
        public String getSuspension() {
            return suspension;
        }
     
        public void setSuspension(String suspension) {
            this.suspension = suspension;
        }
    public void printDescription(){
     
     
    }
     
     
     
     
        }

    main class
    package assignment_3;
     
    import java.util.Scanner;
     
     
    public class TestBikes {
        public void main (String [] args){
     
            Scanner scan = new Scanner (System.in);
            String[]ar = new String [6];
        }
    }


    as you see i'm not good at connecting classes and using them in the main class


    much appreciated any help and thank you


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Having a trouble Making a program

    Please edit your post above and change all [quote] [/quote] tags that are surrounding code blocks to [code] [/code] tags so that we can read your code.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having a trouble Making a program

    Alright, done

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Having a trouble Making a program

    You should not try to solve everything all at once, and I would instead break the problem down into smaller steps and try to solve each step one at a time. These steps can include (but are not limited to):
    1. prompting the user for information
    2. this includes asking the user what type of Bicycle to create, speed, etc...
    3. using that information to create your Bicycle subclass objects
    4. Placing the objects into the array
    5. Trying to run the program to see what will happen.


    Then if you're stuck on a single step, show us your code attempt for that step and ask your questions.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having a trouble Making a program

    Quote Originally Posted by curmudgeon View Post
    You should not try to solve everything all at once, and I would instead break the problem down into smaller steps and try to solve each step one at a time. These steps can include (but are not limited to):
    1. prompting the user for information
    2. this includes asking the user what type of Bicycle to create, speed, etc...
    3. using that information to create your Bicycle subclass objects
    4. Placing the objects into the array
    5. Trying to run the program to see what will happen.


    Then if you're stuck on a single step, show us your code attempt for that step and ask your questions.
    I'm trying to do that, but i'm not good at making programs with subclasses, i have little knowledge in them and our teacher isn't that good at explanation.
    I'm good doing java in 1 class which we took in last year
    i have to submit this program by tonight.

    if someone can tell me the codes i should put in the main method so it become complete
    please help

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Having a trouble Making a program

    Quote Originally Posted by ZeraX View Post
    I'm trying to do that, but i'm not good at making programs with subclasses, i have little knowledge in them and our teacher isn't that good at explanation.
    I'm good doing java in 1 class which we took in last year
    i have to submit this program by tonight.
    You get good by practice.

    if someone can tell me the codes i should put in the main method so it become complete
    please help
    This is not allowed nor appreciated here. Please refrain for asking others to do your homework lest we close this question and ban you from the forum. Again, I suggest that you give it your best try, either that or point out exactly what specific questions you may have.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having a trouble Making a program

    Woah, i asked for help in writing some codes, not asking for doing my whole homework, considering i wrote everything else except not implementing the proper driver class.
    this is java not math problem so i create out of mind non-exist methods to solve it.

    all i want is someone guiding me how to make the main method use its subclasses.

    and this is how i got far, i even forgot how to put the values into the array
    public class TestBikes {
        public void main (String [] args){
     
            Bicycle []bi = new Bicycle[6];
     
     
            Scanner scan = new Scanner (System.in);
            String ar[] = new String [6];
            double speed=100;
     
            for(int j=0;j< ar.length;j++){
            for(int i=0; i<6 ; i++){
            System.out.println("This is the first bike, speed is 100, if you to increase (i) or decrease it (d) ?");
            String ans = scan.nextLine();
            if(ans.equalsIgnoreCase("i")){
                speed ++;
     
            }
               if(ans.equalsIgnoreCase("d")){
                   speed--;
     
            }

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Having a trouble Making a program

    You will want to make sure that your code is indented well -- that each block is indented a standard amount, say 3 spaces, and that the indentation amount is consistent, else it will be very hard for us to understand your code and you to be able to debug it later. For instance, you have nested for loops but have not indented the inner loop.

    So you know that you will need a for loop that loops bi.length times in order to put a new Bicycle into the array. So I see that you have this, but I'm not sure what the purpose of your inner for loop is, why you need it and what you're going to do with it. Inside of that for loop, you will want to send prompts to the user, first to select what type of Bicycle subclass he desires, then what speed he desires, than an other thing he desires. Then you'll want to create one of the subclasses depending on his choice and then modify its properties using the Bicycle setter methods, then put it into the array.

    So in pseudocode:
    for loops from i = 0 to i < length of bi array
      Prompt user for what type of Bicycle they want
      Using if/else blocks or switch/case block, create an object of the type they desire
      Prompt user for modifications to this Bicycle
      Using setter methods update the properties of this Bicycle
      Add Bicycle to the ith item in the bi array.
    end for loop

Similar Threads

  1. trouble with making a simple construct for an array of double type
    By Abadude in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 29th, 2012, 05:05 PM
  2. [SOLVED] Having trouble making calls.
    By ntracing777 in forum Object Oriented Programming
    Replies: 9
    Last Post: June 9th, 2012, 01:05 PM
  3. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  4. So I'm making this program...
    By SkyAphid in forum The Cafe
    Replies: 2
    Last Post: August 29th, 2011, 05:55 PM
  5. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM