0% found this document useful (0 votes)
24 views13 pages

Code

The document contains various Java classes and programs that demonstrate concepts such as grade assignment, BMI calculation, and object-oriented programming with classes like Circle and People. It includes examples of using arrays, methods, constructors, and inheritance. Additionally, it showcases the implementation of the Comparable interface for comparing objects based on specific attributes.

Uploaded by

sawitta2004f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Code

The document contains various Java classes and programs that demonstrate concepts such as grade assignment, BMI calculation, and object-oriented programming with classes like Circle and People. It includes examples of using arrays, methods, constructors, and inheritance. Additionally, it showcases the implementation of the Comparable interface for comparing objects based on specific attributes.

Uploaded by

sawitta2004f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner;
public class AssigningGrade2 {
public static void main(String[] args) {
// input student's total marks into an array
Scanner input = new Scanner([Link]);
[Link]("Please enter number of student: ");
int numberOfStudents = [Link]();
int [] marks = new int [numberOfStudents];
[Link]("address of marks (before): "+marks);
for(int i=0; i<[Link]; i++) {
[Link]("Please enter marks for student["+i+"]: ");
marks[i] = [Link]();
}

// find the max of marks


int max = marks[0];
for(int i=1; i<[Link]; i++) {
if(max<marks[i])
max = marks[i];
}
[Link]("max marks of student: "+max);

// compute grade for each student


char [] grade = new char [numberOfStudents];
[Link]("address of grade (before): "+grade);
for(int i=0; i<[Link]; i++) {
if(marks[i]>=max-10)
grade[i] = 'A';
else if(marks[i]>=max-20)
grade[i] = 'B';
else if(marks[i]>=max-30)
grade[i] = 'C';
else if(marks[i]>max-40)
grade[i] = 'D';
else
grade[i] = 'F';
}

for(int i=0; i<numberOfStudents; i++)


[Link]("Student["+i+"] "+"gets "+marks[i]+" points, "
+ "so that he get grade "+grade[i]);

[Link]("address of marks: "+marks);


[Link]("address of grade: "+grade);

int [] marks_copy = marks;


for(int i=0; i<marks_copy.length; i++)
[Link]("marks_copy["+i+"]: "+marks_copy[i]);
[Link]("address of marks_copy: "+marks_copy);
}

public static char[] computeGrade(int [] points) {


char [] g = new char [[Link]];

return g;
}
}

import [Link];
public class AssigningGrade2 {
public static void main(String[] args) {
// input student's total marks into an array
Scanner input = new Scanner([Link]);
[Link]("Please enter number of student: ");
int numberOfStudents = [Link]();
int [] marks = new int [numberOfStudents];
[Link]("address of marks (before): "+marks);
for(int i=0; i<[Link]; i++) {
[Link]("Please enter marks for student["+i+"]: ");
marks[i] = [Link]();
}

// compute grade for each student

char [] grade = computeGrade(marks);


for(int i=0; i<numberOfStudents; i++)
[Link]("Student["+i+"] "+"gets "+marks[i]+" points, "
+ "so that he get grade "+grade[i]);

[Link]("address of marks: "+marks);


//[Link]("address of grade: "+grade);

int [] marks_copy = marks;


for(int i=0; i<marks_copy.length; i++)
[Link]("marks_copy["+i+"]: "+marks_copy[i]);
[Link]("address of marks_copy: "+marks_copy);
}

public static char[] computeGrade(int [] points) {


char [] g = new char [[Link]];

int max = points[0];


for(int i=1; i<[Link]; i++) {
if(max<points[i])
max = points[i];
}

for(int i=0; i<[Link]; i++) {


if(points[i]>=max-10)
g[i] = 'A';
else if(points[i]>=max-20)
g[i] = 'B';
else if(points[i]>=max-30)
g[i] = 'C';
else if(points[i]>max-40)
g[i] = 'D';
else
g[i] = 'F';
}

return g;
}
}

import [Link];
public class TwoDArray2 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int [][] examResult = new int [5][2];
for(int i=0; i<[Link]; i++) {
[Link]("Student "+(i+1)+"\t");
for(int j=0; j<examResult[i].length; j++) {
[Link]("Assignment "+(j+1)+": ");
examResult[i][j] = [Link]();
[Link]("\t\t");
}
[Link]();
}
[Link]("No.\tAssignment 1\tAssigment 2");
for(int i=0; i<[Link]; i++) {
[Link](i+1);
for(int j=0; j<examResult[i].length; j++) {
[Link]("\t"+examResult[i][j]+"\t");
}
[Link]();
}
}
}
Circle Class
public class Circle { // Prototype, Blueprint of Circle
// properties of circle
double radius;
String color;
// no-arg constructor
Circle(){
[Link] = 1.0;
[Link] = "Black";
}
Circle(double radius){
[Link] = radius;
}
Circle(double radius, String color){
[Link] = radius;
[Link] = color;
}
// Behaviours --> Methods
public double area() {
double area = [Link]*[Link](radius, 2);
return area;
}
public double perimeter() {
double perimeter = 2*[Link]*radius;
return perimeter;
}
}

BMI Program
import [Link];
public class BMI_StructureProgramming {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Please enter your height (meters): ");
double height = [Link]();
[Link]("Please enter your weight (kilograms): ");
double weight = [Link]();
double bmi = weight/[Link](height, 2);
if(bmi<18.5)
[Link]("Underweight");
else if(bmi < 24.9)
[Link]("Normal");
else if(bmi < 29.9)
[Link]("Overweight");
else if(bmi < 34.9)
[Link]("Obese");
else
[Link]("Extremely Obese");
}
}
public class BMI {
//data fields
double height;
double weight;
// constructure
BMI(){}
BMI(double height, double weight){
[Link] = height;
[Link] = weight;
}
// behaviour
public double getBMI() {
return weight/[Link](height,2);
}
}
import [Link];
public class BMI_OOP {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Please enter your height (meters): ");
double h = [Link]();
[Link]("Please enter your weight (kilograms): ");
double w = [Link]();
BMI bmi = new BMI(h, w);
if([Link]()<18.5)
[Link]("Underweight");
else if([Link]() < 24.9)
[Link]("Normal");
else if([Link]() < 29.9)
[Link]("Overweight");
else if([Link]() < 34.9)
[Link]("Obese");
else
[Link]("Extremely Obese");
}
}
import [Link];
public class BMI_OOP2 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
BMI bmi = new BMI();
[Link]("Please enter your height (meters): ");
[Link] = [Link]();
[Link]("Please enter your weight (kilograms): ");
[Link] = [Link]();
if([Link]()<18.5)
[Link]("Underweight");
else if([Link]() < 24.9)
[Link]("Normal");
else if([Link]() < 29.9)
[Link]("Overweight");
else if([Link]() < 34.9)
[Link]("Obese");
else
[Link]("Extremely Obese");
}
}

BMI Class ver II and BMI_OOP2


public class BMI {
//data fields
private double height;
private double weight;
// constructure
BMI(){}
BMI(double height, double weight){
[Link] = height;
[Link] = weight;
}
// behaviour
public double getBMI() {
return weight/[Link](height,2);
}
public void setHeight(double height) {
[Link]= height;
}
public void setWeight(double weight) {
[Link] = weight;
}
public String getStatus() {
if(getBMI()<18.5)
return "Underweight";
else if(getBMI() < 24.9)
return "Normal";
else if(getBMI() < 29.9)
return "Overweight";
else if(getBMI() < 34.9)
return "Obese";
else
return "Extremely Obese";
}
}
import [Link];
public class BMI_OOP2 {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
BMI bmi = new BMI();
[Link]("Please enter your height (meters): ");
[Link]([Link]());
[Link]("Please enter your weight (kilograms): ");
[Link]([Link]());
[Link]([Link]());
}
}

People and Students Class


public class People {
private String fName;
private String lName;
private String citizenID;
public double weight;
public double height;
// default constructor
public People(){
[Link]("Hi Son!\n");
}
public People(String citizenID){
[Link] = citizenID;
}
public People(String citizenID, double weight){
[Link] = citizenID;
[Link] = weight;
}
public People(String fName, String lName, String citizenID, double weight, double height){
[Link] = fName;
[Link] = lName;
[Link] = citizenID;
[Link] = weight;
[Link] = height;
}
public String getCitizenID() {
return citizenID;
}
public String getFName() {
return fName;
}
public void setFName(String fName) {
[Link] = fName;
}
public String getLName() {
return lName;
}
public void setLName(String lName) {
[Link] = lName;
}
public String getInfo() {
return "Citizen ID: "+citizenID;
}
}
public class Students extends People{
private String studentID;
String schoolName;
String departmentName;
int yearOfStudy;
public Students(){
super();
[Link]("Hello Mom!\n");
}
public Students(String studentID, String schoolName, String departmentName, int yearOfStudy) {
[Link] = studentID;
[Link] = schoolName;
[Link] = departmentName;
[Link] = yearOfStudy;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
[Link] = studentID;
}
public String getInfo() {
String output = "Student ID: "+studentID+"\nStudent Name: "+getFName()+" "+getLName()
+ "\nSchool Name: "+schoolName+"\nDepartment Name: "+departmentName;
return [Link]()+"\n"+output;
}
public void getInfo2() {
[Link]("Student ID: "+studentID+"\nStudent Name: "+getFName()+" "+getLName()
+ "\nSchool Name: "+schoolName+"\nDepartment Name: "+departmentName);
}
}

[11/4 13:42] Supachate Innet


GeometricObject Class
public class GeometricObject {
private String color = "White";
private boolean filled = false;
private [Link] dateCreated;
public GeometricObject() {
dateCreated = new [Link]();
}
public String getColor() {
return color;
}
public void setColor(String color){
[Link] = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
[Link] = filled;
}
public [Link] getDateCreated(){
return dateCreated;
}
public String toString() {
return [Link]()+"\n"+
"This object is "+color+"\n"+
"Filled : "+filled+"\n"+
"Created on: "+dateCreated;
}
}
public class GeometricObjectDemo {
public static void main(String[] args) {
GeometricObject geoObj = new GeometricObject();
[Link]([Link]());
}
}

abstrct class GeometricObject


public abstract class GeometricObject {
private String color = "White";
private boolean filled = false;
private [Link] dateCreated;
public GeometricObject() {
dateCreated = new [Link]();
}
public abstract double getArea();
public String getColor() {
return color;
}
public void setColor(String color){
[Link] = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
[Link] = filled;
}
public [Link] getDateCreated(){
return dateCreated;
}
public String toString() {
return [Link]()+"\n"+
"This object is "+color+"\n"+
"Filled : "+filled+"\n"+
"Created on: "+dateCreated;
}
}
public class Circle2 extends GeometricObject {
public double getArea() {
return 1.0;
}
}
public class GeometricObjectDemo {
public static void main(String[] args) {
Circle2 geoObj = new Circle2();
[Link]([Link]());
}
}

Example of today's Assignment


public class Circle3 implements Comparable{
public int radius;
Circle3(){
}
public int compareTo(Object o) {
if(((Circle3)o).radius>[Link])
return 1;
else if(((Circle3)o).radius<[Link])
return -1;
else
return 0;
}
}

จาก interface Comparable ที่กาหนดให้


interface Comparable {
public int compareTo(Object o){

}
}
จงเขียนคลาส Circle และ Orange โดยทั้งสองคลาสเชื่อมกับ Comparable
และให้เขียน comparable ให้สมบูรณ์ กาหนดให้เปรี ยบเทียบวงกลมด้วยรัศมี และเปรี ยบเทียมด้วยน้ าหนัก
Comparable
class Circle implements Comparable<Circle> {
private double radius;

public Circle(double radius) {


[Link] = radius;
}

public int compareTo(Circle otherCircle) {

if ([Link] < [Link]) {


return -1;
} else if ([Link] > [Link]) {
return 1;
} else {
return 0;
}
}
}

class Orange implements Comparable<Orange> {


private double weight;

public Orange(double weight) {


[Link] = weight;
}

public int compareTo(Orange otherOrange) {

if ([Link] < [Link]) {


return -1;
} else if ([Link] > [Link]) {
return 1;
} else {
return 0;
}
}
}

Class Test
public class Test {
public static void main(String[] args) {

Circle circle1 = new Circle(2);


Circle circle2 = new Circle(4);
Orange orange1 = new Orange(2);
Orange orange2 = new Orange(6);

if ([Link](circle2) < 0) {
[Link]("Circle1 has a radius less than Circle2.");
} else if ([Link](circle2) > 0) {
[Link]("Circle1 has a radius more than Circle2.");
} else {
[Link]("Circle1 and Circle2 has a same radius.");
}

if ([Link](orange2) < 0) {
[Link]("Orange1 weighs less than Orange2.");
} else if ([Link](orange2) > 0) {
[Link]("Orange1 weighs more than Orange2.");
} else {
[Link]("Orange1 and Orange2 weigh the same.");
}
}
}

public Orange(double weight) {


[Link] = weight;
}

public int compareTo(Orange otherOrange) {

if ([Link] < [Link]) {


return -1;
} else if ([Link] > [Link]) {
return 1;
} else {
return 0;
}
}
}

GeometricObject
import [Link];

public class GeometricObject {


private String color;
private boolean filled;
private Date dateCreated;
// Constructor
public GeometricObject() {
[Link] = "white";
[Link] = false;
[Link] = new Date();
}

// Getter และ Setter color


public String getColor() {
return color;
}

public void setColor(String color) {


[Link] = color;
}

// Getter และ Setter filled


public boolean isFilled() {
return filled;
}

public void setFilled(boolean filled) {


[Link] = filled;
}

// Getter dateCreated
public Date getDateCreated() {
return dateCreated;
}

public String toString() {


return "Created on " + dateCreated + "\nColor: " + color + "\nFilled: " + filled;
}
}

You might also like