From 3b4875dc53b051a1efeaecfe3c942dc86853372d Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 6 Aug 2026 20:35:03 +0300 Subject: [PATCH 1/3] added parson related to section 3.1 --- source/ch3_javadatatypes.ptx | 101 ++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 859fc5b..51f69cd 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -280,43 +280,7 @@ public class TempConv {

The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9, which would result in 0.

- - - -

- Write a program that converts km to miles. Use the Scanner class to read the input. Note that 1 km is approximately equal to 0.621 miles. -

- - -public class KmToMiles { - public static void main(String[] args) { - // write your code here - } -} - - -
- - - -import java.util.Scanner; -public class KmToMiles { - public static void main(String[] args) { - Double kilometers; - Double miles; - Scanner input; - input = new Scanner(System.in); - System.out.print("Enter distance in kilometers: "); - kilometers = input.nextDouble(); - miles = kilometers * 0.621; - System.out.println(kilometers + " kilometers is equal to " + miles + " miles."); - } -} - - - -
@@ -1171,6 +1135,71 @@ public class HistoMap { + + +

+ Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles. + Drag the blocks into the correct order on the right. +

+
+ + + + + import java.util.Scanner; + + + import Scanner; + + + + + public class KmToMiles { + + + + public static void main(String[] args) { + + + + + Scanner input = new Scanner(System.in); + + + Scanner input = new Scanner(System.out); + + + + + + System.out.print("Enter distance in kilometers: "); + double kilometers = input.nextDouble(); + + + System.out.print("Enter distance in kilometers: "); + double kilometers = input.readDouble(); + + + + + + double miles = kilometers * 0.621; + + + double miles = kilometers / 0.621; + + + + + System.out.println(kilometers + " kilometers is equal to " + miles + " miles."); + + + } + } + + +
+ \ No newline at end of file From a01cde69e23d92514fb55bb9007d212e3cc2e05e Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 6 Aug 2026 21:42:46 +0300 Subject: [PATCH 2/3] added for 3.2 --- source/ch3_javadatatypes.ptx | 182 +++++++++++++++++++++++------------ 1 file changed, 118 insertions(+), 64 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 51f69cd..1933e20 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -280,6 +280,69 @@ public class TempConv {

The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9, which would result in 0.

+ + + +

+ Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles. + Drag the blocks into the correct order on the right. +

+
+ + + + + import java.util.Scanner; + + + import Scanner; + + + + + public class KmToMiles { + + + + public static void main(String[] args) { + + + + + Scanner input = new Scanner(System.in); + + + Scanner input = new Scanner(System.out); + + + + + + double kilometers = input.nextDouble(); + + + double kilometers = input.readDouble(); + + + + + + double miles = kilometers * 0.621; + + + double miles = kilometers / 0.621; + + + + + System.out.println(kilometers + " kilometers is equal to " + miles + " miles."); + + + } + } + + +
@@ -449,6 +512,61 @@ void main() {

In , we first create a Dog object and assign it to an Animal reference (upcasting). Then, we check if the Animal reference is actually pointing to a Dog object before downcasting it back to a Dog reference.

+ + + +

+ Construct a program that safely downcasts a Shape reference to a Circle object and calls a subclass method. + Drag the blocks into the correct order on the right. +

+
+ + + class Shape { + ... + } + + + + class Circle extends Shape { + public void drawCircle() { System.out.println("Circle"); } + } + + + + public class Downcast { + public static void main(String[] args) { + Shape myShape = new Circle(); + + + + + if (myShape instanceof Circle) { + + + if (myShape.equals(Circle)) { + + + + + + Circle myCircle = (Circle) myShape; + myCircle.drawCircle(); + + + Circle myCircle = myShape; + myCircle.drawCircle(); + + + + + } + } + } + + +
+ @@ -1135,70 +1253,6 @@ public class HistoMap { - - -

- Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles. - Drag the blocks into the correct order on the right. -

-
- - - - - import java.util.Scanner; - - - import Scanner; - - - - - public class KmToMiles { - - - - public static void main(String[] args) { - - - - - Scanner input = new Scanner(System.in); - - - Scanner input = new Scanner(System.out); - - - - - - System.out.print("Enter distance in kilometers: "); - double kilometers = input.nextDouble(); - - - System.out.print("Enter distance in kilometers: "); - double kilometers = input.readDouble(); - - - - - - double miles = kilometers * 0.621; - - - double miles = kilometers / 0.621; - - - - - System.out.println(kilometers + " kilometers is equal to " + miles + " miles."); - - - } - } - - -
From 0f25a902e270a504ac8d5817ab5677b2d7c6ab61 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Thu, 6 Aug 2026 22:22:50 +0300 Subject: [PATCH 3/3] fixed spacing --- source/ch3_javadatatypes.ptx | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/source/ch3_javadatatypes.ptx b/source/ch3_javadatatypes.ptx index 1933e20..a9ae75a 100644 --- a/source/ch3_javadatatypes.ptx +++ b/source/ch3_javadatatypes.ptx @@ -308,36 +308,42 @@ public class TempConv { + Scanner input; + Double kilometers; + Double miles; + + + - Scanner input = new Scanner(System.in); + input = new Scanner(System.in); - Scanner input = new Scanner(System.out); + input = new Scanner(System.out); - + - double kilometers = input.nextDouble(); + kilometers = input.nextDouble(); - double kilometers = input.readDouble(); + kilometers = input.readDouble(); - + - double miles = kilometers * 0.621; + miles = kilometers * 0.621; - double miles = kilometers / 0.621; + miles = kilometers / 0.621; - + System.out.println(kilometers + " kilometers is equal to " + miles + " miles."); - + } } @@ -529,8 +535,10 @@ void main() { class Circle extends Shape { - public void drawCircle() { System.out.println("Circle"); } - } + public void drawCircle() { + System.out.println("Circle"); + } + }