Programming Language II: Homework 2 - Solutions for Chapters 9 and 10, Assignments of Java Programming

Introduction to Java Programming Liang 10th Edition Exercises

Typology: Assignments

2021/2022

Uploaded on 01/18/2023

hasanzahid007
hasanzahid007 🇧🇩

2 documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
North South University
Department of Electrical & Computer Engineering
Fall 2022
CSE215: Programming Language II
Homework 2
Submitted By
Zahid Hasan
ID: 1911509042
Section - 6
Submitted To
MR. RIFAT AHMED HASSAN
Lecturer
Department of Electrical &
Computer Engineering
Date of Submission: 2-1-2023
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

Partial preview of the text

Download Programming Language II: Homework 2 - Solutions for Chapters 9 and 10 and more Assignments Java Programming in PDF only on Docsity!

North South University

Department of Electrical & Computer Engineering

Fall 2022

CSE215: Programming Language II

Homework 2

Submitted By

Zahid Hasan

ID: 1911509042

Section - 6

Submitted To

MR. RIFAT AHMED HASSAN

Lecturer

Department of Electrical &

Computer Engineering

Date of Submission: 2-1-

Answer to Chapter – 9, Programming Exercise – 9.

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:

  • point1.getX() + ", " + point1.getY() + ") and ("
  • point2.getX() + ", " + point2.getY() + ") is: " + point1.distance(point2)); } } MyPoint.java public class MyPoint { // Data fields private double x; private double y; MyPoint() { this(0, 0); } MyPoint(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public double distance(MyPoint myPoint) {

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:

Answer to Chapter – 10, Programming Exercise – 10.

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:

Answer to Chapter – 11, Programming Exercise – 11.

UML Diagram: