
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to Java Programming Liang 10th Edition Exercises
Typology: Assignments
1 / 64
This page cannot be seen from the preview
Don't miss anything!

























































import java.util.GregorianCalendar; public class Ch9Ex5 { public static void main(String[] args){ GregorianCalendar calender = new GregorianCalendar(); int month = calender.get(calender.MONTH); int day = calender.get(calender.DAY_OF_MONTH); int year = calender.get(calender.YEAR); System.out.print("\nCurrent year, month, and day in format Day-Month-Year: "); System.out.println(day+"-"+(month+1)+"-"+year); calender.setTimeInMillis(1234567898765L); System.out.print("\nElapsed time since January 1, 1970 set to " + "1234567898765L in format Day-Month-Year: "); System.out.println(calender.get(calender.DAY_OF_MONTH)+"- "+calender.get(calender.MONTH) +"-"+ calender.get(calender.YEAR)); } } Output:
fan2.setSpeed(MEDIUM); fan2.setRadius(5); fan2.setColor("blue"); fan2.turnOff(); System.out.println(fan1.toString()); System.out.println(fan2.toString()); } } Fan.java public class Fan { final static int SLOW = 1; final static int MEDIUM = 2; final static int FAST = 3; private int speed; private boolean on; private double radius; String color; Fan() { speed = SLOW; on = false; radius = 5; color = "blue"; } public void setSpeed(int newSpeed) { speed = newSpeed; } public void turnOn() {
on = true; } public void turnOff() { on = false; } public void setColor(String newColor) { color = newColor; } public void setRadius(double newRadius) { radius = newRadius; } public String getSpeed() { String s = ""; switch (speed) { case SLOW: s = "SLOW"; break; case MEDIUM: s = "MEDIUM"; break; case FAST: s = "FAST"; } return s; } public double getRadius() { return radius; } public String getColor() { return color; }
Test class public class Ch10Ex3 { public static void main(String[] args) { int[] values = {5, 6, 7, 8, 9}; System.out.println("\nTest if values are even using isEven(int):"); for (int i = 0; i < values.length; i++) { System.out.println(values[i] + " " + MyInteger.isEven(values[i])); } System.out.println("\nTest if values are odd using isOdd(int):"); for (int i = 0; i < values.length; i++) { System.out.println(values[i] + " " + MyInteger.isOdd(values[i])); }
System.out.println("\nTest if values are prime using isPrime(int):"); for (int i = 0; i < values.length; i++) { System.out.println(values[i] + " " + MyInteger.isPrime(values[i])); } System.out.println("\nTest if values are even using isEven():"); for (int i = 0; i < values.length; i++) { MyInteger value = new MyInteger(values[i]); System.out.println(value.getValue() + " " + value.isEven()); } System.out.println("\nTest if values are odd using isOdd():"); for (int i = 0; i < values.length; i++) { MyInteger value = new MyInteger(values[i]); System.out.println(value.getValue() + " " + value.isOdd()); } System.out.println("\nTest if values are prime using isPrime():"); for (int i = 0; i < values.length; i++) { MyInteger value = new MyInteger(values[i]); System.out.println(value.getValue() + " " + value.isPrime()); }
System.out.println("\nTest if " + value.getValue() + " is equal to the specified value:"); for (int i = 0; i < values2.length; i++) { MyInteger myInteger = new MyInteger(values2[i]); System.out.println(values2[i] + " " + value.equals(myInteger)); } System.out.println("\nTest parseInt(char[]) and parseInt(String):"); char[] numericCharacters = {'3', '4', '2'}; String numericString = "658"; System.out.print("'"); for (int i = 0; i < numericCharacters.length; i++) { System.out.print(numericCharacters[i] + ""); } System.out.println("' + "" + numericString + "" = "
(MyInteger.parseInt(numericCharacters) + MyInteger.parseInt(numericString))); } } MyInteger.java public class MyInteger { private int value; MyInteger(int value) { this.value = value; } public int getValue() { return value;
public boolean isEven() { return isEven(value); } public boolean isOdd() { return isOdd(value); } public boolean isPrime() { return isPrime(value); } public static boolean isEven(int value) { return value % 2 == 0; } public static boolean isOdd(int value) { return value % 2 != 0; } public static boolean isPrime(int value) { for (int divisor = 2; divisor <= value / 2; divisor++) { if (value % divisor == 0) return false; } return true; } public static boolean isEven(MyInteger myInteger) { return myInteger.isEven();
return value; } } Output:
return Math.sqrt(Math.pow(myPoint.getX() - x, 2) + Math.pow(myPoint.getY() - y, 2)); } public double distance(double x, double y) { return distance(new MyPoint(x, y)); } } Output:
UML Diagram: Test class public class Ch10Ex14 { public static void main(String[] args) { // Create two MyDate objects MyDate date1 = new MyDate();
MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public String getMonth() { String m = String.valueOf(month + 1); return (month < 10? "0" + m : m); } public String getDay() { String d = String.valueOf(day); return (day < 10? "0" + d : d); } public void setDate(long elapsedTime) { GregorianCalendar calander = new GregorianCalendar(); calander.setTimeInMillis(elapsedTime); year = calander.get(GregorianCalendar.YEAR); month = calander.get(GregorianCalendar.MONTH); day = calander.get(GregorianCalendar.DAY_OF_MONTH); } }
Output:
UML Diagram: