Chia sẻ kiến thức
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Giải bài tập

Go down

Giải bài tập Empty Giải bài tập

Bài gửi  Admin Fri Sep 19, 2014 4:14 pm

Chương 2:
Giải bài tập C2_bai10
Bài 1: lớp Điểm
Code:

import java.lang.Math;
import java.util.Scanner;
public class Diem{
   private int x,y;
   public Diem(){
      x=y=0;
   }
   public Diem(int h,int t){
      x=h;
      y=t;
   }
   public void nhapXY(){
      Scanner sc = new Scanner(System.in);
      System.out.print("Nhap hoanh do x: ");
      x=sc.nextInt();
      System.out.print("Nhap hoanh do y: ");
      y=sc.nextInt();
   }
   public void inXY(){
      System.out.println("Toa do (x,y): "+"("+x+","+y+")");
   }
   public int getX(){
      return x;
   }
   public void setX(int x){
      this.x=x;
   }
   public int getY(){
      return y;
   }
   public void setY(int x){
      this.y=y;
   }
   public static float getDistance1(int x1, int y1, int x2, int y2){
      float r;
      r=(float)Math.sqrt((float)Math.pow(x1-x2,2)+(float)Math.pow(y1-y2,2));
      return r;
   }
   public static float getDistance2(Diem A, Diem B){
      float r;
      r=(float)Math.sqrt((float)Math.pow(A.x-B.x,2)+(float)Math.pow(A.y-B.y,2));
      return r;
   }
   public static void main(String args[]){
      Diem A = new Diem();
      A.inXY();
      Diem B = new Diem(8,1);
      B.inXY();
      float R1 = Diem.getDistance1(A.x, A.y, B.x, B.y);
      System.out.println("Khoang cach tu diem A toi diem B la: "+R1);
      float R2 = Diem.getDistance2(A,B);
      System.out.println("Khoang cach tu diem A toi diem B la: "+R2);
   }
}

Bài 2: lớp Phương trình bậc 2
Code:

import java.util.Scanner;
public class PTB2{
   private int a,b,c;
   public PTB2(int a, int b, int c){
      this.a = a;
      this.b = b;
      this.c = c;
   }
   public void nhapABC(){
      Scanner sc = new Scanner(System.in);
      System.out.print("Nhap a: ");
      a = sc.nextInt();
      System.out.print("Nhap b: ");
      b = sc.nextInt();
      System.out.print("Nhap c: ");
      c = sc.nextInt();
   }
   public void inABC(){
      System.out.println("a="+a+","+"b="+b+","+"c="+c);
   }
   public void giaiPTB2(){
      float delta =(float)Math.pow(this.b,2)-4*this.a*this.c;
      if(delta<0) System.out.println("Phuong trinh vo nghiem");
      else if(delta==0){
         float r =-this.b/(2*this.a);
         System.out.print("Phuong trinh co 1 nghiem: "+r);
      }
         else{
            float r1= -this.b+(float)Math.sqrt(delta)/(2*this.a);
            float r2= -r1;
            System.out.print("Phuong trinh co 2 nghiem phan biet la: "+r1+","+r2);
         }
   }
   public static void main(String args[]){
      PTB2 a = new PTB2(1,2,-3);
      a.nhapABC();
      a.inABC();
      a.giaiPTB2();
   }
}
Admin
Admin
Admin

Tổng số bài gửi : 218
Reputation : 22
Join date : 17/11/2012
Age : 32

https://elcit.forumvi.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết