感谢LDingHui同学提供的代码
Lab Report 03
Note:
- All your lab reports should be uploaded to BB before the deadline.
Caution
- Must be original works, to prohibit any copying or plagiarism
一、 Experimental Purposes and Requirements
- to learn how to selections;
- to learn how to loops;
- to learn java API library:Math, String, and Character
二、Experimental Contents
1、(Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following formula:
b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”.
Hint 1: you can use Math.pow(x, 0.5) to compute. Hint 2: you can use the flowing selection syntax.
if(discriminant < 0) {
}else if(discriminant == 0) {
}else {
}
Here are some sample runs.
Test example 1:
Test example 2:
Test example 3:
2、(Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month name January, February, …, December for the number 1, 2, …, 12, accordingly.
Hint 1: you can use Math.random() to generate a random.
Hint 2: you can use the switch selection syntax.
Here are some sample runs.
Test example 1:
Test example 2:
3、(Game: scissor, rock, paper) Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Hint 1: you can use Math.random() to generate a random.
Hint 2: you can use nested switch and if selection syntax like the following code.
Here are sample runs:
Test example 1:
Test example 2:
Test example 3:
4、(Cost of shipping) A shipping company uses the following function to calculate the cost (in dollars) of shipping based on the weight of the package (in pounds).
Write a program that prompts the user to enter the weight of the package and display the shipping cost. If the weight is greater than 20, display a message “the package cannot be shipped.”
Here are sample runs:
Test example 1:
Test example 2:
5、(Geometry: area of a pentagon) Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure.
The formula for computing the area of a pentagon is
where s is the length of a side. The side can be computed using the formula:
where r is the length from the center of a pentagon to a vertex. Round up two digits after the decimal point.
Hint:you can use the methods of Math class.
Here is a sample run:
6、(Find the character of an ASCII code) Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character. Here is a sample run:
Hint: the conversion between integer number and character.
7、(Student major and status) Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following characters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology
Hint1: String s = input.nextLine();
Hint2: s.charAt(0)/ s.charAt(1)
Here is a sample run:
Test example 1:
Test example 2:
8、(Process a string) Write a program that prompts the user to enter a string and displays its length and its last character.
Hint: you can use method s.length() to get the length of a string.
9、(Check substring) Write a program that prompts the user to enter two strings and reports whether the second string is a substring of the first string.
Hint: you can use method s1.indexof(s2).
Test example 1:
Test example 2:
10、(Find the highest score) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name of the student with the highest score.
Test example:
11、(Find numbers divisible by 5 and 6) Write a program that displays all the numbers from 100 to 1,000, ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space.
Hint: you can use for loop.
Test example:
12、(Find the factors of an integer) Write a program that reads an integer and displays all its smallest factors in increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
Test example 1:
Test example 2:
三、Please show your questions analysis, code and results.
1、
import java.util.Scanner;
/**
* @ClassName SolveQuadraticEquations
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:56
**/
public class SolveQuadraticEquations {
public static void main(String[] args) {
double a = 0.0, b = 0.0, c = 0.0, d = 0.0, r1 = 0.0, r2 = 0.0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = (b * b - 4 * a * c);
if (d < 0) {
System.out.println("The equation has no real roots");
} else if (d == 0) {
r1 = (-b + java.lang.Math.sqrt(d)) / 2 * a;
System.out.println("The equation has one real roots " + r1);
} else {
r1 = (-b + java.lang.Math.sqrt(d)) / 2 * a;
r2 = (-b - java.lang.Math.sqrt(d)) / 2 * a;
System.out.println("The equation has one real roots " + r1 + " and " + r2);
}
}
}
2、
/**
* @ClassName RandomMonth
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:57
**/
public class RandomMonth {
public static void main(String[] args) {
int k = (int) (Math.random() * 12) + 1;
switch (k) {
case 1 -> System.out.println("1 Month is January");
case 2 -> System.out.println("2 Month is February");
case 3 -> System.out.println("3 Month is March");
case 4 -> System.out.println("4 Month is April");
case 5 -> System.out.println("5 Month is May");
case 6 -> System.out.println("6 Month is June");
case 7 -> System.out.println("7 Month is July");
case 8 -> System.out.println("8 Month is August");
case 9 -> System.out.println("9 Month is September");
case 10 -> System.out.println("10 Month is October");
case 11 -> System.out.println("11 Month is November");
case 12 -> System.out.println("12 Month is December");
}
}
}
3、
import java.util.Scanner;
/**
* @ClassName ScissorRockPaper
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:57
**/
public class ScissorRockPaper {
public static void main(String[] args) {
int com, you;
com = (int) (Math.random() * 3);
System.out.print("scissor(0), rock(1), paper(2): ");
Scanner sc = new Scanner(System.in);
you = sc.nextInt();
switch (com) {
case 0:
if (you == 0)
System.out.println("The computer is scissor, you are scissor too. It is a draw");
else if (you == 1)
System.out.println("The computer is scissor, you are rock. You won");
else
System.out.println("The computer is scissor, you are paper. You lost");
break;
case 1:
if (you == 0)
System.out.println("The computer is rock, you are scissor. You lost");
else if (you == 1)
System.out.println("The computer is rock, you are rock too. It is a draw");
else
System.out.println("The computer is rock, you are paper. You won");
break;
case 2:
if (you == 0)
System.out.println("The computer is paper, you are scissor. You won");
else if (you == 1)
System.out.println("The computer is paper, you are rock. You lost");
else
System.out.println("The computer is paper, you are paper. It is a draw");
break;
}
}
}
4、
import java.util.Scanner;
/**
* @ClassName CostOfShipping
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:57
**/
public class CostOfShipping {
public static void main(String[] args) {
double weight;
System.out.print("Enter package weight: ");
Scanner sc = new Scanner(System.in);
weight = sc.nextDouble();
if(weight > 0 && weight <= 1)
System.out.println("The shipping cost is $3.5");
else if(weight > 1 && weight <= 3)
System.out.println("The shipping cost is $5.5");
else if(weight > 3 && weight <= 10)
System.out.println("The shipping cost is $8.5");
else if(weight > 10 && weight <= 20)
System.out.println("The shipping cost is $10.5");
else
System.out.println("The package cannot be shipped");
}
}
5、
/**
* @ClassName AreaOfPentagon
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:58
**/
public class AreaOfPentagon {
public static void main(String[] args) {
double r = 0.0 ,area = 0.0 ,s = 0.0;
System.out.print("Enter the length from the center to a vertex: ");
Scanner sc = new Scanner(System.in);
r = sc.nextDouble();
s = 2 * r * Math.sin(Math.PI / 5);
area = 5 * s * s / 4 / Math.tan(Math.PI / 5);
System.out.println("The area of the pentagon is " + area);
}
}
6、
import java.util.Scanner;
/**
* @ClassName CharacterOfASCII
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:58
**/
public class CharacterOfASCII {
public static void main(String[] args) {
int n;
System.out.print("Enter a int: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println("The character for ASCII code " + n + " is " + (char)n);
}
}
7、
import java.util.Scanner;
/**
* @ClassName StudentMajorAndStatus
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:58
**/
public class StudentMajorAndStatus {
public static void main(String[] args) {
System.out.print("Enter two characters: ");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
switch (s.charAt(0)) {
case 'M' -> System.out.print("Mathematics ");
case 'C' -> System.out.print("Computer Science ");
case 'I' -> System.out.print("Information Technology ");
}
switch (s.charAt(1)) {
case '1' -> System.out.println("Freshman");
case '2' -> System.out.println("Sophomore");
case '3' -> System.out.println("Junior");
case '4' -> System.out.println("Senior");
}
}
}
### 8、
import java.util.Scanner;
/**
* @ClassName ProcessString
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:59
**/
public class ProcessString {
public static void main(String[] args) {
int n;
System.out.print("Enter a string: ");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
n = s.length();
System.out.println("The length of the string is " + n);
System.out.println("The last character in the string is " + s.charAt(n-1));
}
}
9、
import java.util.Scanner;
/**
* @ClassName CheckSubstring
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:59
**/
public class CheckSubstring {
public static void main(String[] args) {
System.out.print("Enter string s1: ");
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
System.out.print("Enter string s2: ");
String s2 = sc.nextLine();
boolean re = s1.contains(s2);
if(re)
System.out.println(s2 + " is a substring of " + s1);
else
System.out.println(s2 + " is not a substring of " + s1);
}
}
10、
import java.util.Scanner;
/**
* @ClassName FindHighestScore
* @Author OwemShu
* @Description
* @Date 2021/11/16 8:59
**/
public class FindHighestScore {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of the student: ");
int number = input.nextInt();
int[] score = new int[number];
String[] name = new String[number];
int max = 0;
for (int i = 0; i < number; i++) {
System.out.print("Enter a student name: ");
name[i] = input.next();
System.out.print("Enter a student score: ");
score[i] = input.nextInt();
if (i != 0 && score[i] > score[max])
max = i;
}
System.out.print("the student with highest score is "+ name[max]+"'s score is "+ score[max]);
}
}
11、
/**
* @ClassName FindNumbersDivisible
* @Author OwemShu
* @Description
* @Date 2021/11/16 9:00
**/
public class FindNumbersDivisible {
public static void main(String[] args) {
int n = 100 ,sum=0;
while(n <= 1000) {
if (n%30 == 0) {
sum++;
if(sum % 10 == 0)
System.out.println( n );
else
System.out.print( n + " ");
}
n++;
}
}
}
12、
import java.util.Scanner;
/**
* @ClassName FindFactorsOfInteger
* @Author OwemShu
* @Description
* @Date 2021/11/16 9:00
**/
public class FindFactorsOfInteger {
public static void main(String[] args) {
int n, i;
System.out.print("Enter a positive integer: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println("The facter for " + n + " is");
while( n != 1) {
for(i = 2 ;i <= n ;i++)
{
if( n % i == 0)
{
System.out.print(i + " ");
n = n / i;
break;
}
}
}
}
}