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)
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-03-05 09:17: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 geometricobject;
import java.util.Date;
/**
*
* @author Admin
*/
public abstract class GeometricObject {
String collor;
boolean filles;
Date dateCreated;
public GeometricObject() {
}
public GeometricObject(String collor, boolean filles, Date dateCreated) {
this.collor = collor;
this.filles = filles;
this.dateCreated = dateCreated;
}
public String getCollor() {
return collor;
}
public void setCollor(String collor) {
this.collor = collor;
}
public boolean isFilles() {
return filles;
}
public void setFilles(boolean filles) {
this.filles = filles;
}
public Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "collor=" + collor + ", filles=" + filles + ", dateCreated=" + dateCreated;
}
public abstract double getArea();
public abstract double getPerimeter();
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-03-05 09:17:16
/*
* 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 geometricobject;
/**
*
* @author Admin
*/
public abstract class Circle extends GeometricObject {
double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getDiameter() {
double Diameter = 2 * radius;
return Diameter;
}
@Override
public double getArea() {
double Area = radius * radius * 3.14;
return Area;
}
@Override
public double getPerimeter() {
double Perimeter = (2 * radius) * 3.14;
return Perimeter;
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-03-05 09:16:53
/*
* 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 geometricobject;
/**
*
* @author Admin
*/
public class Rectangle extends GeometricObject{
double width;
double height;
@Override
public double getArea() {
double Area = width * height;
return Area;
}
@Override
public double getPerimeter() {
double Perimeter = 2 * (width + height) ;
return Perimeter;
}
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;
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-03-05 09:16:24
/*
* 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 geometricobject;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class test {
public static void main(String[] args) {
ArrayList<GeometricObject> l= new ArrayList<>();
Circle c = new Circle() {};
Rectangle r = new Rectangle();
Scanner scan = new Scanner (System.in);
System.out.println("Nhap radius");
c.radius = scan.nextDouble();
System.out.println("");
System.out.println("Nhap chieu rong");
r.height = scan.nextDouble();
System.out.println("Nhap chieu dai");
r.width = scan.nextDouble();
l.add(r);
l.add(c);
System.out.println(sumArea(c,r));
}
public static double sumArea(Circle c , Rectangle r){
double t = c.getArea() + r .getArea();
return t;
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-03-05 09:08:56
import java.util.ArrayList;
import java.util.Scanner;
/*
* 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.
*/
/**
*
* @author Administrator
*/
public class Driver {
public static void main(String[] args){
ArrayList<sumArea> list = new ArrayList<>();
System.out.println("Nhap \n");
Scanner input = new Scanner(System.in);
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-03-05 09:08:29
/*
* 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 lesson1;
/**
*
* @author Administrator
*/
public class Circle extends GeometricObject{
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 Area() {
return radius * radius * Math.PI;
}
@Override
public double Perimeter() {
return 2 * radius;
}
public double getDiameter(){
return 2 * radius * Math.PI;
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-03-05 09:08:09
/*
* 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 lesson1;
import static javax.swing.Spring.width;
/**
*
* @author Administrator
*/
public class Retangle extends GeometricObject{
double with;
double height;
public Retangle(){
}
public Retangle(double with, double height) {
this.with = with;
this.height = height;
}
public double getWith() {
return with;
}
public void setWith(double with) {
this.with = with;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double Area() {
return width * height;
}
@Override
public double Perimeter() {
return 2 * (width + height);
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-03-05 09:07:56
/*
* 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 lesson1;
import java.util.Date;
/**
*
* @author Administrator
*/
public abstract class GeometricObject {
String color;
boolean filled;
java.util.Date dateCreate;
public GeometricObject() {
}
public GeometricObject(String color, boolean filled, Date dateCreate) {
this.color = color;
this.filled = filled;
this.dateCreate = dateCreate;
}
public abstract double Area();
public abstract double Perimeter();
public double getArea(double Area){
return Area;
}
public double getPerimeter(double Perimeter){
return Perimeter;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreate() {
return dateCreate;
}
public void setDateCreate(Date dateCreate) {
this.dateCreate = dateCreate;
}
}
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2021-03-05 09:05:09
#Circle.java
/*
* 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 Figure1;
/**
*
* @author dong
*/
public class Circle extends GeometricObject {
private double radius;
public Circle(){
}
public Circle(double radius){
this.radius = radius;
}
public Circle(double radius, String color, boolean filled)
{
this.radius = radius;
setColor(color);
setFilled(filled);
}
//getter
public double getRadius()
{
return radius;
}
@Override
public double getArea()
{
return radius * radius * Math.PI;
}
@Override
public double getPerimeter()
{
return 2 * radius;
}
public double getDiameter()
{
return 2 * radius * Math.PI;
}
//setter
public void setRadius( double radius)
{
this.radius = radius;
}
//print the Circle
public void printCircle()
{
System.out.println("Tạo hình tròn thành công " + getDateCreated() + " Bán kính " + radius);
}
}
#GeometricObject.java
/*
* 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 Figure1;
import java.util.Date;
/**
*
* @author dong
*/
public abstract class GeometricObject {
private String color;
private boolean filled;
private java.util.Date DateCreated ;
public GeometricObject(){
}
public GeometricObject(String colour, boolean filled, Date datetime) {
this.color = colour;
this.filled = filled;
this.DateCreated = datetime;
}
public String getColour(){
return color;
}
public void setColor(String color) {
this.color = color;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public void setDatetime(Date DateCreated) {
this.DateCreated = DateCreated;
}
public boolean isFilled() {
return filled;
}
public Date getDateCreated() {
return DateCreated;
}
/**
*
* @return
*/
public abstract double getArea();
public abstract double getPerimeter();
}
#Main.java
/*
* 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 Figure1;
/**
*
* @author dong
*/
public class Main {
public static double sumArea(GeometricObject[] a){
double sum = 0;
for (GeometricObject a1 : a) {
sum += a1.getArea();
}
return sum;
}
public static void main(String[] args) {
GeometricObject[] array = {new Circle(5), new Circle(8),
new Rectangle(3, 4), new Rectangle(4, 2)};
// Display results
System.out.println("Tổng tất cả là: " + sumArea(array));
}
}
#Rectangle.java
/*
* 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 Figure1;
/**
*
* @author dong
*/
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
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea()
{
return width * height;
}
//get perimeter
@Override
public double getPerimeter(){
return 2 * (width + height);
}
}
![Vũ Đình Khôi [community,T2008A]](https://www.gravatar.com/avatar/522a3ab049e7409705e97b96dbbc327b.jpg?s=80&d=mm&r=g)
Vũ Đình Khôi
2021-03-05 09:04:57
/*
* 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 exem1;
/**
*
* @author mac
*/
public class GeometricObject {
//date fields
private String color = "White";
private boolean filled;
private java.util.Date dateCreated;
//constructors
public GeometricObject()
{
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
//getter
public String getColor()
{
return color;
}
public boolean isFilled()
{
return filled;
}
public java.util.Date getDateCreated()
{
return dateCreated;
}
//setter
public void setColor(String color)
{
this.color = color;
}
public void setFilled(boolean filled)
{
this.filled = filled;
}
//override toString
public String toString()
{
return "Created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
/*
* 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 exam1;
/**
*
* @author mac
*/
public class Circle extends GeometricObject {
//data field
private double radius;
//constructors
public Circle(){
}
public Circle(double radius){
this.radius = radius;
}
public Circle(double radius, String color, boolean filled)
{
this.radius = radius;
setColor(color);
setFilled(filled);
}
//getter
public double getRadius()
{
return radius;
}
public double getArea()
{
return radius * radius * Math.PI;
}
public double getPerimeter()
{
return 2 * radius;
}
public double getDiameter()
{
return 2 * radius * Math.PI;
}
//setter
public void setRadius( double radius)
{
this.radius = radius;
}
//print the Circle
public void printCircle()
{
System.out.println("The circle is created " + getDateCreated() + " and the radius is " + 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 exam1;
/**
*
* @author mac
*/
public class Rectangle {
double width, 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;
}
}
/*
* 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 exam1;
/**
*
* @author mac
*/
import java.util.*;
public class main {
//print out menu
public static int getMenuOption(Scanner inputScanner) throws IllegalArgumentException
{
if(inputScanner != null)
{nnn
while(true)
{
try
n {
//get the option
System.out.print("(1)Add (2)Sort (3)Print (0)Exit: ");
int option = inputScanner.nextInt();
if(option < 0 || option >= 4)
System.out.println("Incorrect input: 0, 1, 2, or 3 is required\n");
else
return option;
}
//when input error occurs
catch(InputMismatchException ex)
{
System.out.println("Incorrect input: 0, 1, 2, or 3 is required\n");
inputScanner.nextLine();
}
}
}
//when argument have problem
else
{
throw new IllegalArgumentException("Incorrect input: 0, 1, 2, or 3 is required");
}
}
//add triangle to the list
public static void addTriangle(Scanner inputScanner,
ArrayList<Triangle> list) throws IllegalArgumentException
{
if(inputScanner != null || list != null)
{
Triangle tri;
while(true)
{
//add triangle
try
{
System.out.print("Enter three sides: ");
tri = new Triangle(inputScanner.nextDouble(), inputScanner.nextDouble(), inputScanner.nextDouble());
break;
}
//when input error happens
catch(InputMismatchException ex)
{
System.out.println("Incorrect input: double value is required");
inputScanner.nextLine();
}
//when arguments have problem
catch(IllegalArgumentException ex1)
{
System.out.println(ex1.getMessage());
}
}
//decide color
System.out.print("Enter the color: ");
inputScanner.nextLine();
tri.setColor(inputScanner.nextLine());
while(true)
{
//decide if it is filled
try
{
System.out.print("Enter a boolean value for filled: ");
tri.setFilled(inputScanner.nextBoolean());
break;
}
//when input error happens
catch(InputMismatchException ex1)
{
System.out.println("Incorrect input: boolean value is required ");
inputScanner.nextLine();
}
}
list.add(tri);
System.out.println("");
}
//when arguments have problem
else
throw new IllegalArgumentException("");
}
//insertion sort the list by their area
public static void sortTriangles(ArrayList<Triangle> list)
{
Triangle triTem;
for(int i = 1; i < list.size(); i ++)
{
for(int j = 0; j < list.size(); j++)
{
if(list.get(i).getArea() < list.get(j).getArea())
{
triTem = list.get(i);
for(int k = i; k > j; k--)
{
list.set(k, list.get(k - 1));
}
list.set(j, triTem);
}
}
}
System.out.println("Insertion sort performed!\n");
}
//print out the triangle(s) in the list
public static void printTriangles(ArrayList<Triangle> list)
{
switch(list.size())
{
case 0:
System.out.println("The triangle list is empty!\n");
break;
case 1:
System.out.println("The triangle list has 1 triangle:\n" + list.get(0).toString() + "\n");
break;
default:
System.out.println("The triangle list has " + list.size() + " triangles: ");
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i).toString());
}
System.out.println("");
}
}
public static void main(String[] args)
{
ArrayList<Triangle> list = new ArrayList<Triangle>();
System.out.println("CS332 Triangle List\n");
Scanner input = new Scanner(System.in);
int ex;
// 1 is add, 2 is sort, 3 is print, 0 is exit
do
{
ex = getMenuOption(input);
switch(ex)
{
case 1:
addTriangle(input, list);
break;
case 2:
sortTriangles(list);
break;
case 3:
printTriangles(list);
}
} while(ex != 0);
System.out.println("Goodbye!");
input.close();
}
}
Nam đã gửi Hôm nay lúc 15:51
1 bài nhưng chia ra nhiều lớp á
Nam đã gửi Hôm nay lúc 15:51
/*
* 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 exam1;
/**
*
* @author mac
*/
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);
}
}
Nam đã gửi Hôm nay lúc 15:51
/*
* 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 exam1;
/**
*
* @author mac
*/
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;
}
}