






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 / 11
This page cannot be seen from the preview
Don't miss anything!







import java.util.Scanner; public class Ex$2_6 { public static void main(String[] args){ System.out.print("Enter a number between 0 and 1000: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); int firstDigit, secondDigit, thirdDigit; thirdDigit = num%10; num = num/10; secondDigit = num%10; num = num/10; firstDigit = num%10; System.out.println("the sum of the digits: "+(firstDigit+secondDigit+thirdDigit)); } } Output:
import java.util.Scanner; public class Ex$3_23 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a point with two coordinates: "); double x = input.nextDouble(); double y = input.nextDouble(); if (x > (10/2.0) && y > (5/2.0)){ System.out.print("Point ("+x+","+y+") is not in the rectangle"); } else { System.out.print("Point ("+x+","+y+") is in the rectangle"); } } } Output:
import java.util.Scanner; public class Ex$4_18 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two characters: "); String user = input.nextLine(); char key1 = user.charAt(0); char key2 = user.charAt(1); String major = ""; String type = ""; int check = 0; switch(key1){ case 'M': major = "Mathematics"; break; case 'C': major = "Computer Science"; break; case 'I':
Output:
import java.util.Scanner; public class Ex$4_21 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a SSN: "); String ssn = input.nextLine(); boolean isValid = (Character.isDigit(ssn.charAt(0))) && (Character.isDigit(ssn.charAt(1))) &&
(Character.isDigit(ssn.charAt(2))) && (ssn.charAt(3) == '-') && (Character.isDigit(ssn.charAt(4))) && (Character.isDigit(ssn.charAt(5))) && (ssn.charAt(6) == '-') && (Character.isDigit(ssn.charAt(7))) && (Character.isDigit(ssn.charAt(8))) && (Character.isDigit(ssn.charAt(9))) && (Character.isDigit(ssn.charAt(10))) && (ssn.length() == 11); System.out.println(ssn + " is " + ((isValid)? "a valid " : "an invalid ")
public class Ex$6_12 { public static void main(String[] args){
public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter an integer: "); int num = input.nextInt(); reverse(num); } public static void reverse(int number){ int rem, rev = 0; while(number>0){ rem = number%10; rev = rev*10 + rem; number = number/10; } System.out.println("Reversed integer: "+rev); } } Output:
public class Ex$6_9 { public static void main(String[] args){ double foot, meter; System.out.println("Feet Meters | Meters Feet"); System.out.println("---------------------------------- ---");
for(foot = 1, meter = 20; foot<=10; foot++,meter=meter+5){ System.out.printf("%4.1f %6.3f | %4.1f %6.3f",foot,footToMeter(foot),meter,meterToFoot(meter)); System.out.println(); } } public static double footToMeter(double foot){ double output = foot0.305; return output; } public static double meterToFoot(double meter){ double output = meter3.279; return output; } } Output: