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 Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2021-03-05 09:02:55
/*
* 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 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() {
return 0;
}
public double getDiameter() {
return radius * 2;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-03-05 09:02:52
/*
* 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 Rectangle {
double width, height;
public Rectangle() {
}
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);
}
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;
}
public double getArena() {
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
private void setColor(String color) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setFilled(boolean filled) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:02: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 project2;
/**
*
* @author ADMIN
*/
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();
}
}
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2021-03-05 09:02:42
/*
* 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 GeometricObject {
String colour;
boolean filled;
dateCreated datetime;
public GeometricObject() {
}
private static class dateCreated {
}
public GeometricObject(String colour, boolean filled, dateCreated datetime) {
this.colour = colour;
this.filled = filled;
this.datetime = datetime;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public dateCreated getDatetime() {
return datetime;
}
public void setDatetime(dateCreated datetime) {
this.datetime = datetime;
}
@Override
public String toString() {
return "GeometricObject{" + "colour=" + colour + ", filled=" + filled + ", datetime=" + datetime + '}';
}
public double getArena() {
return 0;
}
public double Perimeter() {
return 0;
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-03-05 09:02:39
/*
* 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 Circle {
double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double readius, String color, boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getDiameter() {
return 2 * radius;
}
public double getArena() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius;
}
public void printCircle() {
System.out.println("Created " + getDateCreated() + " and " + radius);
}
private String getDateCreated() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setColor(String color) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setFilled(boolean filled) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:02:36
/*
* 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 {
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;
}
}
![bui duy khanh [T2008A]](https://www.gravatar.com/avatar/50b4bb73ad99c982b2c18af8cf07e2a4.jpg?s=80&d=mm&r=g)
bui duy khanh
2021-03-05 09:02:32
kết quả bài thi thực hành.
/*
* 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;
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-03-05 09:02:25
/*
* 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;
import java.util.Date;
/**
*
* @author Admin
*/
public class GeometricObject {
String color = "Red";
boolean filled;
java.util.Date dateCreated;
public GeometricObject() {
dateCreated = new java.util.Date();
}
public GeometricObject(boolean filled, Date dateCreated) {
this.filled = filled;
this.dateCreated = dateCreated;
dateCreated = new java.util.Date();
}
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 getDateCreated() {
return dateCreated;
}
public String toString(){
return "Created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:02: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 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);
}
}
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2021-03-05 09:01: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 project2;
/**
*
* @author ADMIN
*/
public class GeometricObject {
String color;
boolean filled;
dateCreated datetime;
private static class dateCreated {
public dateCreated() {
}
}
public GeometricObject() {
}
public GeometricObject(String color, boolean filled, dateCreated datetime) {
this.color = color;
this.filled = filled;
this.datetime = datetime;
}
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 dateCreated getDatetime() {
return datetime;
}
public String toString() {
return null;
}
public double getArena() {
return 0;
}
public double getPerimeter() {
return 0;
}
}