By GokiSoft.com|
19:42 06/05/2024|
Java Basic
[Examination] ADF1 - Java Basic - T2008A
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2021-03-05 09:04:42
GeometricObject
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Examination;
import java.util.Date;
/**
*
* @author NAV
*/
public class GeometricObject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
private String color = "White";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject()
{
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor()
{
return color;
}
public boolean isFilled()
{
return filled;
}
public java.util.Date getDateCreated()
{
return dateCreated;
}
public void setColor(String color)
{
this.color = color;
}
public void setFilled(boolean filled)
{
this.filled = filled;
}
public String toString()
{
return "Created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-03-05 09:04:08
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package examjavabasic;
/**
*
* @author TrungDuc
*/
public class Rectange extends GeometricObject {
Float Width;
Float Height;
public Rectange() {
}
public Rectange(Float Width, Float Height, String Color, Boolean Filled, String dateCreated) {
super(Color, Filled, dateCreated);
this.Width = Width;
this.Height = Height;
}
public Float getWidth() {
return Width;
}
public void setWidth(Float Width) {
this.Width = Width;
}
public Float getHeight() {
return Height;
}
public void setHeight(Float Height) {
this.Height = Height;
}
@Override
public String getColor() {
return Color;
}
@Override
public void setColor(String Color) {
this.Color = Color;
}
@Override
public Boolean getFilled() {
return Filled;
}
@Override
public void setFilled(Boolean Filled) {
this.Filled = Filled;
}
@Override
public String getDateCreated() {
return dateCreated;
}
@Override
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
@Override
public float getArea() {
return (float) (Height * Width);
}
@Override
public float perimeter() {
return (float) (2 * (Height + Width));
}
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2021-03-05 09:03:48
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exam;
/**
*
* @author UserName
*/
public class GeometricObject {
String colour;
boolean filled;
dateCreated datetime;
private static class dateCreated {
}
public GeometricObject() {
}
public GeometricObject(String colour, boolean filled, dateCreated datetime) {
this.colour = colour;
this.filled = filled;
this.datetime = datetime;
}
public double getArea();
public double getDiameter();
public double getPerimeter();
public String getColour() {
return colour;
}
public boolean isFilled() {
return filled;
}
public dateCreated getDatetime() {
return datetime;
}
public void setColour(String colour) {
this.colour = colour;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public void setDatetime(dateCreated datetime) {
this.datetime = datetime;
}
@Override
public String toString() {
return "GeometricObject{" + "colour=" + colour + ", filled=" + filled + ", datetime=" + datetime + '}';
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exam;
import static java.lang.Math.PI;
/**
*
* @author UserName
*/
public class Circle extends GeometricObject {
public double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return radius*radius*PI;
}
@Override
public double getPerimeter() {
return 2*radius*PI;
}
@Override
public double getDiameter() {
return 2*radius;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exam;
import static java.lang.Math.PI;
/**
*
* @author UserName
*/
public class Rectangle extends GeometricObject{
public double width;
public double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return width*height;
}
@Override
public double getPerimeter() {
return 2*(width + height);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exam;
import java.util.ArrayList;
/**
*
* @author UserName
*/
public class Driver {
public static void main(String[] args) {
ArrayList<GeometricObject> list = new ArrayList<>();
Circle c = new Circle();
c.radius = 10;
c.colour = "white";
c.filled = true;
Rectangle r = new Rectangle();
r.height = 100;
r.width = 200;
r.colour = "red";
r.filled = true;
list.add(c);
list.add(r);
System.out.println(c);
System.out.println(r);
sumArea(c, r);
}
public static double sumArea(Circle c, Rectangle r) {
double cArea = c.getArea();
double rArea = r.getArea();
double sum = cArea + rArea;
System.out.println("Ket qua la: " + sum);
return 0;
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-03-05 09:03:44
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package examjavabasic;
/**
*
* @author TrungDuc
*/
public class Circle extends GeometricObject {
Float Radius;
public Circle() {
}
public Circle(String Color, Boolean Filled, String dateCreated, float Radius) {
super(Color, Filled, dateCreated);
this.Radius = Radius;
}
public float getDiameter(){
return 2* this.Radius;
}
public Float getRadius() {
return Radius;
}
public void setRadius(Float Radius) {
this.Radius = Radius;
}
@Override
public String getColor() {
return Color;
}
@Override
public void setColor(String Color) {
this.Color = Color;
}
@Override
public Boolean getFilled() {
return Filled;
}
@Override
public void setFilled(Boolean Filled) {
this.Filled = Filled;
}
@Override
public String getDateCreated() {
return dateCreated;
}
@Override
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
@Override
public float getArea() {
return (float) (3.14*Radius*Radius);
}
@Override
public float perimeter() {
return (float) (2*Radius);
}
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:03:31
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package project2;
/**
*
* @author ADMIN
*/
public class Rectangle extends GeometricObject{
//date field
private double width;
private double height;
//constructors
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color, boolean filled)
{
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
//getter
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
//setter
private void setHeight(double height)
{
this.height = height;
}
public double getArea()
{
return width * height;
}
//get perimeter
public double getPerimeter(){
return 2 * (width + height);
}
}
'
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-03-05 09:03:27
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package examjavabasic;
/**
*
* @author TrungDuc
*/
public abstract class GeometricObject {
String Color;
Boolean Filled;
String dateCreated;
public GeometricObject() {
}
public GeometricObject(String Color, Boolean Filled, String dateCreated) {
this.Color = Color;
this.Filled = Filled;
this.dateCreated = dateCreated;
}
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public Boolean getFilled() {
return Filled;
}
public void setFilled(Boolean Filled) {
this.Filled = Filled;
}
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
public abstract float getArea();
public abstract float perimeter();
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:03:14
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package project2;
/**
*
* @author ADMIN
*/
public class Triangle extends GeometricObject{
//date fields
private double side1;
private double side2;
private double side3;
//constructors
public Triangle()
{
this(1.0, 1.0, 1.0);
}
public Triangle(double side1, double side2, double side3) throws IllegalArgumentException
{
if(side1 > 0 && side2 > 0 && side3 > 0 && ((side1 + side2 > side3) || (side2 + side3 > side1) || (side1 + side3 > side2)))
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
else
{
throw new IllegalArgumentException(
"Invalid sides for triangle: (" + side1 + ", " + side2 + ", " + side3 + ")");
}
}
//setters
public void setSide1(double side1)
{
this.side1 = side1;
}
public void setSide2(double side2)
{
this.side2 = side2;
}
public void setSide3(double side3)
{
this.side3 = side3;
}
//getters
public double getArea()
{
double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p-side3));
}
//get perimeter
public double getPerimeter()
{
return side1 + side2 + side3;
}
//override toString
public String toString()
{
String fill = "not filled";
if(isFilled())
fill = "filled";
String s = "Triangle (" + side1 + ", " + side2 + ", " + side3 + "): "
+ getColor() + ", " + fill + ", " + "area = " + getArea();
return s;
}
}
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2021-03-05 09:03:11
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exam;
/**
*
* @author WIN10
*/
public class Rectangle extends GeometricObject {
double width;
double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-03-05 09:03:11
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package examjavabasic;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author TrungDuc
*/
public class Driver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<GeometricObject> listObject = new ArrayList<>();
System.out.println("Nhap ban kinh hinh tron: ");
float Radius = Float.parseFloat(scan.nextLine());
Circle circle = new Circle();
circle.setRadius(Radius);
listObject.add(circle);
System.out.println("Nhap chieu rong hinh chu nhat: ");
float Width = Float.parseFloat(scan.nextLine());
System.out.println("Nhap chieu cao hinh chu nhat: ");
float Height = Float.parseFloat(scan.nextLine());
Rectange rectange = new Rectange();
rectange.setHeight(Height);
rectange.setWidth(Width);
listObject.add(rectange);
sumArea(circle, rectange);
}
static void sumArea(Circle circle, Rectange rectange) {
float circleArea = circle.getArea();
float circleRectange = rectange.getArea();
float sumArea = circleArea + circleRectange;
System.out.println("Tong dien tich 2 hinh la: " + sumArea);
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-03-05 09:03:07
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package baithithuchanh;
/**
*
* @author Admin
*/
public class Diver extends GeometricObject{
double side1;
double side2;
double side3;
public Diver() {
}
public Diver(double side1, double side2, double side3) throws IllegalArgumentException {
if(side1 > 0 && side2 > 0 && side3 > 0 && ((side1 + side2 > side3) || (side2 + side3 > side1) || (side1 + side3 > side2))) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
} else {
throw new IllegalArgumentException("Invalid sides for triangle: (" + side1 + ", " + side2 + ", " + side3 + ")");
}
}
}