Program that prompts the user to enter the first 9 digits of an ISBN number and displays the 10-digit ISBN…
Program that randomly generates an addition question with two integers less than 100.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class Ex03_10 {<br /> public static void main(String[] args) {<br /> // Generate two integers less than 100<br /> int number1 = (int) (Math.random() * 100);<br /> int number2 = (int) (Math.random() * 100);<br /><br /> // Prompt the user to answer "What is number1 + number2?"<br /> System.out.print("What is " + number1 + " + " + number2 + "? ");<br /> Scanner input = new Scanner(System.in);<br /> int answer = input.nextInt();<br /> // Grade the answer and display the result<br /> if (number1 + number2 == answer) {<br /> System.out.println("You are correct!");<br /> } else {<br /> System.out.println("Your answer is wrongn" + number1 + " + " + number2 + " is "<br /> + (number1 + number2));<br /> }<br /> }<br />}<br /> |
Sample program runClick here to see other solutions…
Program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is…
Program that generates two integers under 100 and prompts the user to enter the sum of these two integers.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class Ex03_04 {<br /> public static void main(String[] args) {<br /> int number1 = (int)(System.currentTimeMillis() % 100);<br /> int number2 = (int)(System.currentTimeMillis() / 7 % 100);<br /> Scanner input = new Scanner(System.in);<br /> System.out.print("What is " + number1 + " + " + number2 + "? ");<br /> int answer = input.nextInt();<br /> System.out.println(number1 + " + " + number2 + " = " + answer + " is " <br /> + (number1 + number2 == answer));<br /> } <br />}<br /> |
Sample runClick…
The program below is the solution to Liang’s Introduction to Java (9th Edition) Chapter 3 Exercise 3.3.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br />import java.util.Scanner;<br /><br />public class Ex03_03 {<br /> public static void main(String[] args) {<br /> // declare variables<br /> double a, b, c, d, e, f, num1, num2, denom, x, y;<br /> <br /> // create Scanner to read user input<br /> Scanner input = new Scanner(System.in);<br /><br /> System.out.print("This program solves 2x3 system of linear equations. "<br /> + "nnFor example in the following equation 9x + 4y = -6 "<br /> + "n 3x - 5y = -21"<br /> + "na, b, c, d, e, f = 9, 4, 3, -5, -6, -21 respectively. "<br /> + "nEnter each number separated by pressing space or enter.");<br /> <br /> // prompt user to enter details<br /> System.out.print("nnEnter a, b, c, d, e, f: ");<br /> a = input.nextDouble();<br /> b = input.nextDouble();<br /> c = input.nextDouble();<br /> d = input.nextDouble();<br /> e = input.nextDouble();<br /> f = input.nextDouble();<br /> <br /> //calculate x and y<br /> num1 = ((e * d) - (b * f));<br /> num2 = ((a * f) - (e * c));<br /> denom = ((a * d) - (b * c));<br /> <br /> x = num1 / denom;<br /> y = num2 / denom;<br /> <br /> if (denom == 0) {<br /> System.out.println("nThe equation has no solution.");<br /> } else {<br /> System.out.println("x is " + x + " and y is " + y);<br /> }<br /> }<br />}<br /> |
Program sample runClick here…
Just came across a program I wrote then in school to calculate area of triangle using Hero’s formula.Program flowchart
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> */ <br /><br />Program AreaOfTriangle;<br />const<br />PI = 3.142;<br />type<br />r=array [1..3] of real; {hold the values of sides of triangle}<br />var<br />d:r;<br />A:real;<br />K:integer;<br />s:real;<br />begin<br /> For k:= 1 to 10 Do {loop for the 10 sets of values}<br /> begin<br /> writeln ('Enter Values of first side');<br /> read (d[1]);<br /> writeln ('Enter Values of second side');<br /> read (d[2]);<br /> writeln ('Enter Values of third side');<br /> read (d[3]);<br /> s:= (d[1]+d[2]+d[3])/2; {compute s}<br /> A:= Sqrt(s*((s-d[1])*(s-d[2])*(s-d[3]))); {compute area}<br /> writeln ('Area = ', A);<br /> end<br />end. <br /> |
The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 3 Exercise 3.2.Question: The program…
The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 3 Exercise 3.1.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> * Exercise 3.1 - Algebra: Solve Quadratic Equations<br /> */<br />import java.util.Scanner;<br /><br />public class CheckerboardPatternOfAsterisks {<br /> public static void main (String [] args) {<br /> <br /> // declare variables<br /> double a, b, c, disc, r1, r2;<br /> <br /> // create Scanner to read user input<br /> Scanner input = new Scanner(System.in);<br /><br /> // prompt user to enter details<br /> System.out.print("Enter a, b, c: ");<br /> a = input.nextDouble();<br /> b = input.nextDouble();<br /> c = input.nextDouble();<br /><br /> //calculate the discriminant<br /> disc = b * b - (4 * a * c);<br /> <br /> //calculate the roots<br /> r1 = (-b + Math.pow(disc, 0.5)) / (2 * a);<br /> r2 = (-b - Math.pow(disc, 0.5)) / (2 * a);<br /><br /> // display the results<br /> if (disc > 0){<br /> System.out.println("The roots are " + r1 + " and " + r2 + "n");<br /> }<br /> <br /> if (disc == 0){<br /> System.out.println("The root is " + r1 + "n");<br /> }<br /> <br /> if (disc < 0){<br /> System.out.println("The equation has no real roots.n");<br /> }<br /> }<br />}<br /> |
Sample Program runClick…
The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.27.Question: Rewrite Exercise…
The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.26.Question: Rewrite Listing…
The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.25.Question: Write a…
The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.23.Question: Write a…