A certain grade of steel is graded according to the following conditions


Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel

  1. Hardness must be greater than 50
  2. Carbon content must be less than 0.7
  3. Tensile strength must be greater than 5600

The grades are as follows:

  • Grade is 10 if all three conditions are met
  • Grade is 9 if conditions (i) and (ii) are met
  • Grade is 8 if conditions (ii) and (iii) are met
  • Grade is 7 if conditions (i) and (iii) are met
  • Grade is 6 if only one condition is met
  • Grade is 5 if none of the conditions are met

  • The program starts by importing the Scanner class from the java.util package, which is used to read user input from the console. It then defines the main method, which is the entry point for the program.
  • Inside the main method, the program prompts the user to enter the values of the hardness, carbon content, and tensile strength using the System.out.print statement and reads these values using the Scanner.nextFloat method.
  • The program then defines three variables (hs_f, cc_f, and ts_f) to represent boolean flags that will be used to determine the steel grade. These variables are initialized to zero (0).
  • Next, the program uses a series of if statements to set the values of the boolean flags based on the input values. For example, if the hardness value is greater than 50, the hs_f flag is set to 1. If the carbon content value is less than 0.7, the cc_f flag is set to 1. If the tensile strength value is greater than 5600, the ts_f flag is set to 1.
  • After setting the boolean flags, the program uses another set of if statements to determine the steel grade based on the values of the boolean flags. If none of the flags are set, the steel grade is set to 5. If one or more flags are set, the steel grade is set to 6. If the hs_f and ts_f flags are set but the cc_f flag is not set, the steel grade is set to 7. If the cc_f and ts_f flags are set but the hs_f flag is not set, the steel grade is set to 8. If the hs_f and cc_f flags are set but the ts_f flag is not set, the steel grade is set to 9. Otherwise, the steel grade is set to 10.
  • Finally, the program uses the System.out.println statement to output the steel grade to the console. The output includes the string "The Grade of Steel :" followed by the value of the grade variable.

Source Code

import java.util.Scanner;
class Steel_Grade
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The value of Hardness :");
		float hs = input.nextFloat();
		System.out.print("Enter The value of Carbon Content :");
		float cc = input.nextFloat();
		System.out.print("Enter The value of Tensile Strength :");
		float ts = input.nextFloat();
		float hs_f=0,cc_f=0,ts_f=0,grade;
		if(hs>50) 
			hs_f=1;	
		if(cc<0.7) 
			cc_f=1;
		if(ts>5600) 
			ts_f=1;
		if(hs_f==0 && cc_f==0 && ts_f==0) 
			grade = 5;
		else if(hs_f==1 || cc_f==1 || ts==1) 
			grade = 6;
		else if(hs_f==1 && cc_f==0 && ts_f==1) 
			grade = 7;
		else if(hs_f==0 && cc_f==1 && ts_f==1)
			grade = 8;
		else if(hs_f==1 && cc_f==1 && ts_f==0) 
			grade = 9;
		else
			grade = 10;
		System.out.println("The Grade of Steel  :"+ grade);
 
	}
}

Output

Enter The value of Hardness :24.23
Enter The value of Carbon Content :21.4
Enter The value of Tensile Strength :23
The Grade of Steel  :5.0

Example Programs