ive got most of this figured out. the problem is then when i run, instead of my methods iterating through my array list, they simply reuse the same data set over and over, giving the same results. if anyone can help, it is appreciated.

/**
 * Write a description of class CatapultTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
public class CatapultTester
{
    public static void main(String[] args)
       {
           ArrayList<Catapult> catapultInfo = new ArrayList<Catapult>();
            catapultInfo.add(new Catapult(20, 0.43));
            catapultInfo.add(new Catapult(25, 0.52));
            catapultInfo.add(new Catapult(30, 0.61));
            catapultInfo.add(new Catapult(35, 0.69));
            catapultInfo.add(new Catapult(40, 0.78));
            catapultInfo.add(new Catapult(45, 0.87));
            catapultInfo.add(new Catapult(50, 0.95));
            
            
            
            //print header
            System.out.println("                                                 Projectile Distance (feet)");
            System.out.println("  MPH \t 25 deg \t 30 deg \t 35 deg \t 40 deg \t 45 deg \t 50 deg \t 55 deg");
            System.out.println("===============================================================================================================");
            
            
            
            for (int counter = 0; counter < catapultInfo.size(); counter++)  
                {  
                    System.out.printf(" %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f", String.valueOf(catapultInfo.get(counter).distanceFeet) +  
                    String.valueOf(catapultInfo.get(counter).distanceFeet) + String.valueOf(catapultInfo.get(counter).distanceFeet) +  
                    String.valueOf(catapultInfo.get(counter).distanceFeet) + String.valueOf(catapultInfo.get(counter).distanceFeet) +  
                    String.valueOf(catapultInfo.get(counter).distanceFeet) + String.valueOf(catapultInfo.get(counter).distanceFeet) +  
                    String.valueOf(catapultInfo.get(counter).distanceFeet));  
       
                }  
           
        }
}
/**
 * Write a description of class Catapult here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Catapult
{
    public double launchSpeed;
    public double launchAngle;
    public double distance;
    public static final double accelerationDueToGravity = 9.8;
    public static final double speedFeetFactor = 3.28;
    public double distanceFeet;
    
    
    

    public Catapult(double speed, double angle)
    {
        distance = speed * speed * Math.sin(2 * angle) / accelerationDueToGravity;
        distanceFeet = distance * speedFeetFactor;
    }
    
    public double calcDistanceMeters()
    {
        return distance;
        
    }
    
    public double calcDistanceFeet()
    {
        return distanceFeet;
    }
}

Recommended Answers

All 4 Replies

Sorry I dont why are you printing the same value multiple times in your for loop. The value of counter will only increase once And why are you using printf??? If you want to print only till two places of decimal better use NumberFormatter class.And if you want all of them printed in one line use print statement.
Probally this might be what you want.

/**
 * Write a description of class CatapultTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
import java.text.*;
public class CatapultTester
{
    public static void main(String[] args)
       {
           ArrayList<Catapult> catapultInfo = new ArrayList<Catapult>();
            catapultInfo.add(new Catapult(20, 0.43));
            catapultInfo.add(new Catapult(25, 0.52));
            catapultInfo.add(new Catapult(30, 0.61));
            catapultInfo.add(new Catapult(35, 0.69));
            catapultInfo.add(new Catapult(40, 0.78));
            catapultInfo.add(new Catapult(45, 0.87));
            catapultInfo.add(new Catapult(50, 0.95));
            
            
            
            //print header
            System.out.println("                                                 Projectile Distance (feet)");
            System.out.println("MPH\t25 deg\t30 deg\t35 deg\t 40 deg\t 45 deg\t 50 deg\t 55 deg");
            System.out.println("===============================================================================================================");
            
            
             NumberFormat formatter = new DecimalFormat("0.00");
            for (int counter = 0; counter < catapultInfo.size(); counter++)  
                {  
                    System.out.print("\t"+formatter.format(catapultInfo.get(counter).distanceFeet));
                     
       
                }  
           
        }
}

i updated my class like so:

/**
 * Write a description of class CatapultTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
public class CatapultTester
{
    public static void main(String[] args)
       {
           ArrayList<Catapult> catapultInfo = new ArrayList<Catapult>();
            catapultInfo.add(new Catapult(20, 0.43));
            catapultInfo.add(new Catapult(25, 0.52));
            catapultInfo.add(new Catapult(30, 0.61));
            catapultInfo.add(new Catapult(35, 0.69));
            catapultInfo.add(new Catapult(40, 0.78));
            catapultInfo.add(new Catapult(45, 0.87));
            catapultInfo.add(new Catapult(50, 0.95));
            
            
            
            //print header
            System.out.println("                                                 Projectile Distance (feet)");
            System.out.println("  MPH \t 25 deg \t 30 deg \t 35 deg \t 40 deg \t 45 deg \t 50 deg \t 55 deg");
            System.out.println("===============================================================================================================");
            
            NumberFormat formatter = new DecimalFormat("0.00");
            
            for (int counter = 0; counter < catapultInfo.size(); counter++)  
                {  
                    System.out.print("\t"+formatter.format(catapultInfo.get(counter).distanceFeet));
                   
       
                }  
           
        }
}

when i try to compile i get cannot find symbol class number format

i tested out the program without the number formatter as it was causing errors, and i got results like so:

Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg
===============================================================================================================
101.4581063794493 180.40088427029016 282.8797244748864 402.5599474526674 535.478994670088 668.0762026352801 791.8021141874283

the only problem with these is that for one: im trying to print the miles per hour under the mph header and its supposed to fill the chart. thats really what my original problem was, i could get the first row to column to print, but im trying to get 6 more lines to print after this one.

i updated my class like so:

/**
 * Write a description of class CatapultTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
public class CatapultTester
{
    public static void main(String[] args)
       {
           ArrayList<Catapult> catapultInfo = new ArrayList<Catapult>();
            catapultInfo.add(new Catapult(20, 0.43));
            catapultInfo.add(new Catapult(25, 0.52));
            catapultInfo.add(new Catapult(30, 0.61));
            catapultInfo.add(new Catapult(35, 0.69));
            catapultInfo.add(new Catapult(40, 0.78));
            catapultInfo.add(new Catapult(45, 0.87));
            catapultInfo.add(new Catapult(50, 0.95));
            
            
            
            //print header
            System.out.println("                                                 Projectile Distance (feet)");
            System.out.println("  MPH \t 25 deg \t 30 deg \t 35 deg \t 40 deg \t 45 deg \t 50 deg \t 55 deg");
            System.out.println("===============================================================================================================");
            
            NumberFormat formatter = new DecimalFormat("0.00");
            
            for (int counter = 0; counter < catapultInfo.size(); counter++)  
                {  
                    System.out.print("\t"+formatter.format(catapultInfo.get(counter).distanceFeet));
                   
       
                }  
           
        }
}

when i try to compile i get cannot find symbol class number format

You missed to import java.text.* class for Numberformat

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.