This web page requires JavaScript to be enabled.

JavaScript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.

How to enable JavaScript?

Tuần 8: Giao diện (interface)

10cth1-2 April 27, 2012 0

[codes=java]import java.util.*;
import java.io.*;

interface HINH
{
  public final double PI=3.14;
  public void Nhap();
  public void Xuat();
  public float Dientich();  
}

class HINHVUONG implements HINH
{
  private float canh;
  public void Nhap()
  {
    Scanner x=new Scanner(System.in);
    System.out.print("NHap canh cho hinh vuong: ");
    canh=x.nextFloat();
  }
  public void Xuat()
  {
    System.out.println("Hinh vuong co canh vua nhap la:" +canh);    
  }
  public float Dientich()
  {
    return canh*canh;
  }
}

class HCN implements HINH
{
  private float dai;
  private float rong;
  public void Nhap()
  {
    Scanner x=new Scanner(System.in);
    System.out.print("Nhap chieu dai cho HCN: ");
    dai=x.nextFloat();
    System.out.print("Nhap chieu rong cho HCN: ");
    rong=x.nextFloat();
  }
  public void Xuat()
  {
    System.out.println("Hinh chu nhat co chieu dai la:" +dai);
    System.out.println("Hinh chu nhat co chieu rong la:" +rong);    
  }
  public float Dientich()
  {
    return dai*rong;
  }
}

class HT implements HINH
{
  private float bankinh;
  public void Nhap()
  {
    Scanner x=new Scanner(System.in);
    System.out.print("Nhap ban kinh hinh tron: ");
    bankinh=x.nextFloat();
  }
  public void Xuat()
  {
    System.out.println("Hinh tron co ban kinh la:" +bankinh);    
  }

  public float Dientich()
  {
    return bankinh*bankinh;
  }
}

class DEMO
{
  public static void main (String[] args)
  {
    int n;
    Scanner x=new Scanner(System.in);
    System.out.print("Nhap so hinh:");
    n=x.nextInt();
    int chon;
    HINH a[]=new HINH[n];
    for(int i=0; i<n; i++)
    {
      do{
        System.out.println("1. Hinh vuong: ");
        System.out.println("2. Hinh chu nhat:");
        System.out.println("3. Hinh tron:");
        System.out.print("Nhap chon:");
        chon=x.nextInt();
        switch(chon)
        {
          case 1: a[i]=new HINHVUONG(); a[i].Nhap(); break;
          case 2: a[i]=new HCN(); a[i].Nhap(); break;
          case 3: a[i]=new HT(); a[i].Nhap(); break;
          default: System.out.println("Ban nhap sai nhap lai."); break;
        }
      }while(chon<1 || chon>3);
    }
    System.out.print("\n\n");
    for(int i=0; i<n; i++)
    {
      System.out.println("\nHinh thu " +(i+1)+ " la:");
      a[i].Xuat();
      System.out.print("Dien tich" +a[i].Dientich());
    }
  }
}[/codes]


Last modified on December 4th, 2020 at 11:31 pm

vietnam1992
vietnam1992



0 responds

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.