Java: Xây dựng lớp Tam giác được xác định bởi 3 Điểm trong Mặt phẳng

Người đăng: share-nhungdieuhay on Thứ Hai, 24 tháng 6, 2013


1. Hãy xây dựng lớp Diem cùng với các đối tượng điểm trong mặt phẳng và các phương thức sau:
        - Phương thức khởi tạo
        - Phương thức in một đối tượng Diem
        - Tính khoảng cách giữa hai điểm
2. Mỗi tam giác trong mặt phẳng được xác định bởi 3 điểm. Hãy xây dựng lớp TamGiac với 3 thuộc tính riêng là 3 đối tượng thuộc lớp Diem và các phương thức:
        - Các phương thức khởi tạo
        - Tính diện tích tam giác
        - Tính chu vi của tam giác
-------------------------------------------------- 
import java.io.*;
class Diem{
        protected double hd,td;
        Diem(){}
        Diem(double a, double b){
               hd=a;
               td=b;
        }
        void in(){
               System.out.println("("+hd+","+td+")");
        }
        double tinhkc(Diem d1, Diem d2){
               double kc=0;
               kc=Math.sqrt(Math.pow(d1.hd-d2.hd,2)+Math.pow(d1.td-d2.td,2));
               return kc;
        }     
}
public class TamGiac{
        private Diem A = new Diem();
        private Diem B = new Diem();
        private Diem C = new Diem();
        double AB,BC,AC;
        TamGiac(){}
        TamGiac(Diem d1, Diem d2, Diem d3){
               A=d1;
               B=d2;
               C=d3;
        }     
        double Chuvi(){
               double cv=0;
               cv=AB+BC+AC;
               return cv;
        }
        double Dientich(){
               double dt;
               float p;
               p=(float)(AB+BC+AC)/2;
               dt=Math.sqrt(p*(p-AB)*(p-AC)*(p-BC));
               return dt;
        }
        static String nhapgt() throws IOException{
                String str;
               InputStreamReader isr = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(isr);
               str= br.readLine();
               return str;
        }
        static Diem nhapdiem(String x) throws IOException{
               int a,b;
               System.out.println("Diem "+x);
               System.out.print("Hoanh do: ");
               a=Integer.valueOf(nhapgt()).intValue();
               System.out.print("Tung do: ");
               b=Integer.valueOf(nhapgt()).intValue();
               Diem nd = new Diem(a,b);
               return nd;
        }
        static TamGiac nhaptgiac() throws IOException{
               Diem d1=new Diem();
               Diem d2=new Diem();
               Diem d3=new Diem();
               d1=nhapdiem("A");
               d2=nhapdiem("B");
               d3=nhapdiem("C");
               TamGiac tg=new TamGiac(d1,d2,d3);
               tg.AB=d1.tinhkc(tg.A,tg.B);
               tg.AC=d1.tinhkc(tg.A,tg.C);
               tg.BC=d1.tinhkc(tg.B,tg.C);
               return tg;
        }     
        public static void main(String args[]) throws IOException{
               TamGiac TG = new TamGiac();
               TG = nhaptgiac();
               System.out.println("Chu vi: "+ TG.Chuvi());
               System.out.println("Dien tich : "+ TG.Dientich());
        }     
}
More about

Java: Viết chương trình Giải phương trình bậc 2.

Người đăng: share-nhungdieuhay


import java.io.*;
public class Gptb2{
        public static void main(String args[]){
               InputStreamReader isr = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(isr);
               try{
                       System.out.println("Nhap gia tri a");
                       String stra = br.readLine();
                       float a = Float.valueOf(stra).floatValue();
                      
                       System.out.println("Nhap gia tri b");
                       String strb = br.readLine();
                       float b = Float.valueOf(strb).floatValue();
                      
                       System.out.println("Nhap gia tri c");
                       String strc = br.readLine();
                       float c = Float.valueOf(strc).floatValue();

                       float delta = b*b - 4*a*c;
                       if(a==0)
                              if(b==0)
                                    if(c==0){
                                                System.out.print("PT co vo so nghiem");
                                    }
                                    else{
                                                System.out.print("PT vo nghiem");
                                    }
                              else{
                                    System.out.println("PT co nghiem duy nhat: " +c/b);
                              }
                       else{
                              if(delta<0){
                                    System.out.println("PT vo nghiem");
                              }
                              else if(delta>0){
                                    float x1 = (float)(-b + Math.sqrt(delta))/(2*a);
                                    float x2 = (float)(-b - Math.sqrt(delta))/(2*a);
                                    System.out.println("PT co 2 nghiem: x1= " +x1 + " va " +x2);
                              }
                              else{
                                    float x = (float)(-b)/(2*a);
                                    System.out.println("PT co nghiem kep: " +x);
                              }
                       }                      
               }
               catch(IOException ie){
                       System.out.println("Error: " +ie);
               }
        }
}
More about

Java: Viết chương trình kiểm tra 1 số được nhập từ bàn phím có phải là số Thuận nghịch không

Người đăng: share-nhungdieuhay on Thứ Tư, 12 tháng 6, 2013


import java.io.*;
public class Test_Tng{
        public static void main(String args[]){
                InputStream is = System.in;
               InputStreamReader isr = new InputStreamReader(is);
               BufferedReader br = new BufferedReader(isr);
               try{
                       String n = br.readLine();
                       long l = Long.valueOf(n).longValue();
                       long k=l, p=0;
                      
                       while(k!=0){
                              p=p*10 + k%10;
                              k=k/10;
                       } 
                       if(l==p) System.out.print(l + " la so thuan nghich ");
                       else System.out.print(l + "khong la so thuan nghich");           
               }
               catch(IOException ie){
                       System.out.println("Error: " +ie);
               }            
        }

}
More about

Java: Viết chương trình kiểm tra 1 số được nhập từ bàn phím có phải là số Nguyên tố hay không

Người đăng: share-nhungdieuhay on Thứ Ba, 11 tháng 6, 2013


import java.io.*;
public class Test_Ngto{
        public static void main(String args[]){
               InputStream is = System.in;
               InputStreamReader isr = new InputStreamReader(is);      
               BufferedReader br = new BufferedReader(isr);
               try{
                       String n = br.readLine();
                       long l = Long.valueOf(n).longValue();
                       long range = (long)Math.sqrt(l);

                       int ok = 1;
                       for(int i=2;i<=range;i++){
                              if(l % i == 0) ok = 0; break;
                       }
                      
                       if(ok == 1){
                              System.out.println("So nay la nguyen to");
                       }
                       else{
                              System.out.println("So nay khong la nguyen to");
                       }
               }
               catch(IOException ie){
                       System.out.print("Error: " +ie);
               }
        }

}
More about

Java: Viết chương trình nhập vào một chuỗi ký tự. Đổi chuỗi thành chữ in hoa.

Người đăng: share-nhungdieuhay on Thứ Hai, 10 tháng 6, 2013

import java.io.*;
public class ReadLine{
   public static void main(String args[]){
           InputStreamReader isr = new InputStreamReader(System.in);
           BufferedReader br = new BufferedReader(isr);
           try{
                        String line = br.readLine();                                        
                        System.out.print(line.toUpperCase());
                       
           }
           catch(IOException ie){
                        System.out.print("Error: " +ie);
           }
          
   }

}
More about

Cài đặt: Hướng dẫn cài đặt Visual Studio 2012 bằng hình ảnh

Người đăng: share-nhungdieuhay on Chủ Nhật, 9 tháng 6, 2013

Bài viết hướng dẫn cài đặt Visual Studio 2012 Ultimate bằng hình ảnh chi tiết.
Tải về bản dùng thử Visual Studio 2012 tại trang chủ của MS: Visual Studio 2012 downloads
Sau khi tải về Visual Studio 2012, giải nén và được bộ cài đặt như sau:

Click vào file vs_ultimate để tiến hành cài đặt

Chọn đường dẫn cài đặt sau đó chọn đồng ý với các điều khoản và quy định để cài đặt
 
Lựa chọn các tùy chọn sẽ được cài đặt, mặc định chọn tất cả

Chương trình sẽ tự động cài đặt vào máy tính

Màn hình hiển thị thông báo cài đặt thành công

Lựa chọn ngôn ngữ mặc định khi chạy chương trình lần đầu tiên

Cửa sổ giao diện chính của chương trình



More about

Code C#: Thêm Ellipse, TextBlock vào cây (Tree)

Người đăng: share-nhungdieuhay


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">
<TreeView>
  <TreeViewItem Header="A" IsExpanded="True">
    <TreeViewItem Header="B" />
    <TreeViewItem Header="C" IsExpanded="True">
      <TreeViewItem Header="D" />
      <TreeViewItem Header="E" />
    </TreeViewItem>
    <TreeViewItem Header="F" />
  </TreeViewItem>
  <TreeViewItem IsExpanded="True">
    <TreeViewItem.Header>
      <StackPanel Orientation="Horizontal">
        <Ellipse Fill="Red" Width="20" Height="20" />
        <TextBlock Text="Third top-level item" />
        <Ellipse Fill="Red" Width="20" Height="20" />
      </StackPanel>
    </TreeViewItem.Header>
    <TreeViewItem Header="Child a" />
    <TreeViewItem Header="Child b" />
    <TreeViewItem Header="Child c" />
  </TreeViewItem>
</TreeView>
</Page>
More about

Code C#: Đưa hình ảnh vào thanh Trạng Thái (StatusBar)

Người đăng: share-nhungdieuhay on Thứ Tư, 5 tháng 6, 2013


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="StatusBarSimple.Window1" Title ="StatusBar">
  <Window.Resources>
    <Style x:Key="StatusBarSeparatorStyle" TargetType="Separator">
      <Setter Property="Background" Value="LightBlue" />
      <Setter Property="Control.Width" Value="1"/>
      <Setter Property="Control.Height" Value="20"/>
    </Style>    
  </Window.Resources>
        <StatusBar Name="sbar" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
                   VerticalAlignment="Bottom" Background="Beige" > 
             <StatusBarItem>
                <Button Content="click" Click="MakeProgressBar"/>
             </StatusBarItem>
             <StatusBarItem>
               <Separator Style="{StaticResource StatusBarSeparatorStyle}"/>
             </StatusBarItem>
        </StatusBar>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StatusBarSimple{
    public partial class Window1 : Window {
        private void MakeProgressBar(object sender, RoutedEventArgs e){
            sbar.Items.Clear();
            DockPanel dpanel = new DockPanel();
            TextBlock txtb = new TextBlock();
            txtb.Text = "Printing  ";
            dpanel.Children.Add(txtb);
            Image printImage = new Image();
            printImage.Width = 20;
            printImage.Height = 20;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(@"pack://application:,,,/images/print.bmp");
            bi.EndInit();
            printImage.Source = bi;
            dpanel.Children.Add(printImage);
            TextBlock txtb2 = new TextBlock();
            txtb2.Text = "5pgs";
            dpanel.Children.Add(txtb2);
            StatusBarItem sbi = new StatusBarItem();
            sbi.Content = dpanel;
            sbi.HorizontalAlignment = HorizontalAlignment.Right;
            ToolTip ttp = new ToolTip();
            ttp.Content = "Sent to printer.";
            sbi.ToolTip = (ttp);
            sbar.Items.Add(sbi);
        }
    }
}
More about

Code C#: Thêm các item (mục) vào Combo box

Người đăng: share-nhungdieuhay on Chủ Nhật, 2 tháng 6, 2013


using System;
using System.Drawing;
using System.Windows.Forms;
public class Select : Form {
  private Button draw = new Button();
  private ComboBox color = new ComboBox();

  public Select( ) {
    draw.Text = "Draw";
    color.Text = "Choose a color";
    Size = new Size(400,240);

    int w = 20;
    draw.Location = new Point(20,30);
    color.Location = new Point(w += 10 + color.Width, 30);

    color.Items.Add("Black");
    color.Items.Add("Red");
    color.Items.Add("Blue");

    Controls.Add(draw);
    Controls.Add(color);

    draw.Click += new EventHandler(Draw_Click);
  } 

  protected void Draw_Click(Object sender, EventArgs e) {
    if (color.SelectedItem.ToString() == "Red" )
      Console.WriteLine("It is red.");
    else if (color.SelectedItem.ToString() == "Red")
      Console.WriteLine("It is Red.");
    else
      Console.WriteLine("It is blue.");
  }
  static void Main() {
    Application.Run(new Select());
  }
}

More about

Code C#: Ràng buộc một TabControl đến nguồn dữ liệu

Người đăng: share-nhungdieuhay on Thứ Bảy, 1 tháng 6, 2013


<Window x:Class="TabControlUsingItemTemplate.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:TabControlUsingItemTemplate"
    Title="TabControlUsingItemTemplate" Height="250" Width="250">
  <Window.Resources>
    <ObjectDataProvider x:Key="TabListResource" ObjectType="{x:Type src:TabList}" />
    <DataTemplate x:Key="HeaderTemplate">
      <TextBlock Text="{Binding Path=Header}" />
    </DataTemplate>
    <DataTemplate x:Key="ContentTemplate">
      <TextBlock Text="{Binding Path=Content}" />
    </DataTemplate>
  </Window.Resources>

  <DockPanel>
    <TabControl ItemsSource="{Binding Source={StaticResource TabListResource}}"
                  ItemTemplate="{StaticResource HeaderTemplate}"
                  ContentTemplate="{StaticResource ContentTemplate}"/>

  </DockPanel>

</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace TabControlUsingItemTemplate
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
    public class TabItemData
    {
        private string _header;
        private string _content;

        public TabItemData(string header, string content)
        {
            _header = header;
            _content = content;
        }
        public string Header
        {
            get { return _header; }
        }
        public string Content
        {
            get { return _content; }
        }
    }
    public class TabList : ObservableCollection<TabItemData>
    {
        public TabList(): base()
        {

            Add(new TabItemData("Header 1", "Content 1"));
            Add(new TabItemData("Header 2", "Content 2"));
            Add(new TabItemData("Header 3", "Content 3"));

        }
    }
}

More about