Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that accepts two integers from the user representing the nu

ID: 3554151 • Letter: W

Question

Write a Java program that accepts two integers from the user representing the number of hits and number of at-bats for a baseball player. Your program should calculate and display the player's hitting percentage. Your code should also check to see if the players hitting percentage is above or equal to 0.300 and display a message indicating the player is eligible for the all-star game if this is the case. If the percentage is below 0.300, display a message wishing the player better luck next year.

This is a Java test program and all the code should be written in a main method.


*Here is a starter code.


import javax.swing.JOptionPane;


public class IfElseBattingAverage

{

public static void main(String[] args)

{

int hits;

int atBats;

int average;

  

hits = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of hits") );

System.out.println(hits);


atBats = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of hits") );


}

}

Explanation / Answer

import javax.swing.JOptionPane;


public class IfElseBattingAverage

{

public static void main(String[] args)

{

int hits;

int atBats;

double average; // since average is decimal, double must be used


hits = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of hits") );

System.out.println(hits);


atBats = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of hits") );

average = (double)atBats/(double)hits;

if(average>=.3){

JOptionPane.showMessageDialog(null, "Your atBat average is "+average+" You are eligible for the all-star game");

}

else{

JOptionPane.showMessageDialog(null, "Your atBat average is "+average+" Better luck next time");

}


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote