⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 righttrianglemain.java

📁 Java code where user can enter the height and base of a triangle and the program will calculate the
💻 JAVA
字号:
import java.util.Scanner ;
import java.lang.Math ;

class RightTriangle {
   private double base   = 0.0 ;
	private double height = 0.0 ;
	
	RightTriangle() {
	   base   = askForDoubleVersion2("Enter base of triangle: ") ;
		height = askForDoubleVersion2("Enter height of triangle: ") ;
	}
	
	RightTriangle (double base, double height) {
	   setBase   (base) ;
		setHeight (height) ;
	}
	
	public static double askForDoubleVersion2(String message) {
   Scanner keyboard ;
   double aValue ;

      keyboard = new Scanner(System.in) ;
	   System.out.print(message) ;
	   aValue = keyboard.nextDouble() ;
	  
	   return aValue ;	 
	}

   public double getBase() {
	   return base ;
	}
	
	public double getHeight() {
	   return height ;
	}

   public boolean isValid() {
	   boolean returnValue = false ;
		   if ((base > 0.0) && (height > 0.0))
		      returnValue = true ;
		return returnValue ;
	}

	public void setBase(double inBase) {
	   if (inBase > 0)
		   base = inBase ;
	}
		
	public void setHeight(double inHeight) {
	   if (inHeight > 0)
		   height = inHeight ;
	}

   double returnArea() {
	   double area ;
		area = (base * height) * 0.5 ;
		return area ;
	}
	
	double returnPerimeter() {
	   double perimeter ;
		double hypotenuse ;
			hypotenuse = Math.sqrt((base * base) + (height * height)) ;
			perimeter  = base + height + hypotenuse ;
			return perimeter ;
	}	
}
	
class RightTriangleMain {		
	public static void displayMenu() {
	   System.out.println("1. Create a right triangle") ;
		System.out.println("2. Display the triangle's area.") ;
		System.out.println("3. Display the triangle's perimeter.") ;
		System.out.println("4. Exit the program.") ;
	}
	

	public static void main(String[] args) {
   RightTriangle theRightTriangle = null ;
	Scanner keyboard               = null ;
	int enteredValue ;
	
	   keyboard = new Scanner(System.in) ;
		do {
		   System.out.println("Enter a choice from the menu:") ;
			displayMenu() ;
			enteredValue = keyboard.nextInt() ;
			
			switch (enteredValue) {
			   case 1: theRightTriangle = new RightTriangle() ;
				        break ;
				case 2: System.out.println("The area is " + theRightTriangle.returnArea()) ;
				        break ;
				case 3: System.out.println("The perimeter is " + theRightTriangle.returnPerimeter()) ;
				        break ;
			}
			
      } while (enteredValue != 4) ;
	
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -