Hi everyone. I am new to this website and I could use some major help. Basically, I am taking AP Computer Science and my teacher has no idea what he is doing. The school thinks that since he can do web design and adobe teaching, he can do java programming. The poor guy is struggling and he can barely help me. This is currently my homework assignment and it's kind of a project grade. I struggle a lot with Java but I am trying my best, so if anyone can help me I would greatly appreciate it. Here are the requirements.

  • Enter "hits" as input.
  • Enter "plate appearances" as input.
  • Compute batting average (baseball)
  • If under 0.3, display a funny message saying that they can do better and display the average.
  • If over 0.3, display a funny message saying that they are doing a really good job and display the average.
  • Display an error if hits is more than plate appearences.

Like I said, my teacher is clueless and I just rely on my textbook, which is the Fundamentals of Java. Here's what I have so far. I also have to use I/O user interface windows and we're still in the unit "Getting Started with Java."

package chapter4project;

import javax.swing.*;

public class Chapter4Project{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double average;
        double panda;

        String inputStr = JOptionPane.showInputDialog("Enter the amount of hits:");
        if (inputStr == null)
            return;
        double hits = Double.parseDouble(inputStr);
        
        inputStr = JOptionPane.showInputDialog("Enter the amount of plate appearances:");
        if (inputStr == null)
            return;
        double PA = Double.parseDouble(inputStr);

       average = hits / PA;

        if (hits > PA){
            JOptionPane.showMessageDialog(null, "This is impossible. Stop cheating.");
        }else if (PA > hits){
            average = (hits / PA);
            JOptionPane.showMessageDialog(null, "Your batting " +
                    "average is " + average + ".");
        
        }else if (average < 3.0){
            JOptionPane.showMessageDialog(null, "Awesome job so far! Your batting average" +
                    "is" + average + ".");

        }else if (average < 0.3){
            JOptionPane.showMessageDialog(null, "You kind of suck. :-( Your batting average" +
                    "is" + average + ".");
        }
    }
}
Member Avatar for Dukane

The part with he funny message should be in it's own separate if/then statement. You also need to proofread and correct some logical mistakes.

For example, take a look at this corrected code:

import javax.swing.*;

public class Chapter4Project{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double average;

        String inputStr = JOptionPane.showInputDialog("Enter the amount of hits:");
        if (inputStr == null)
            return;
        double hits = Double.parseDouble(inputStr);
        
        inputStr = JOptionPane.showInputDialog("Enter the amount of plate appearances:");
        if (inputStr == null)
            return;
        double PA = Double.parseDouble(inputStr);

       average = hits / PA;

        if (hits > PA){
            JOptionPane.showMessageDialog(null, "This is impossible. Stop cheating.");
            System.exit(1);
        }
        
        if (average >= 0.3) {
            JOptionPane.showMessageDialog(null, "Awesome job so far! Your batting average is " +
                    "is " + average + ".");
        }else if (average < 0.3){
            JOptionPane.showMessageDialog(null, "You kind of suck. :-( Your batting average is " +
                    "is " + average + ".");
        }
    }
}

Thanks for the help. I totally get it now. :-)

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.