java语言程序设计基础篇第8版课后答案
【篇一:java语言程序设计基础篇第八章第十题编程参考答案】
icequation的类。这个类包括: 代表三个系数的私有数据域a、b、c。 一个参数为a、b、c的构造方法。 a、b、c的三个get方法。 一个名为getdiscriminant()的方法返回判别式,b2-4ac。 一个名为getroot1()和getroot2()的方法返回等式的两个根。 这些方法只有在判别式为非负数时才有用。如果判别式为负,方法返回0。 画出该类的uml图。实现这个类。编写一个测试程序,提示用户输入a、b、c的值,然后显示判别式的结果。如果判别式为正数,显示两个根;如果判别式为0,显示一个根;否则,显示“the equation has no roots”。
代码:
class quadraticequation{
private int a,b,c;
quadraticequation(){
}
public quadraticequation(int a,int b,int c){
this.a=a;
this.b=b;
this.c=c;
}
public int geta(){
return a;
}
public int getb(){
return b;
}
public int getc(){
return c;
}
public int getdiscriminant(){
if(b*b-4*a*c=0)
return b*b-4*a*c;
else
return 0;
}
public int getroot1(){
if(b*b-4*a*c=0)
return (int)((-b+math.pow(b*b-4*a*c, 0.5))/(2*a));
else
return 0;
}
public int getroot2(){
if(b*b-4*a*c=0)
else
return 0;
}
}
public class xiti810 {
public static void main(string[] args){
system.out.println(请输入要计算的方程的系数a、b和c:);java.util.scanner input =new java.util.scanner(system.in);system.out.print(a=);
int int();
system.out.print(b=);
int int();
system.out.print(c=);
int int();
quadraticequation q=new quadraticequation(a,b,c);
q.getdiscriminant();
discriminant()0)
system.out.println(它们的根为:+q.getroot1()+和+q.getroot2());
else discriminant()==0)
system.out.println(此方程只有一个根为:+q.getroot1());字符串长度17模式串长度8
else
system.out.println(方程无解);
}
}
【篇二:java语言程序设计(第8版)第5章完整答案programming exercises(程序练习题)答案完整版】
class exercise01 {
public static void main(string[] args) {
final int pentagonal_numbers_per_line = 10;
final int pentagonal_numbers_to_print = 100;
int count = 1;
int n = 1;
while (count = pentagonal_numbers_to_print) {
int pentagonalnumber = getpentagonalnumber(n);
n++;
if (count % pentagonal_numbers_per_line == 0)
system.out.printf(%-7d\n, pentagonalnumber);
else
system.out.printf(%-7d, pentagonalnumber);
count++;
}
}
public static int getpentagonalnumber(int n) {
return n * (3 * n - 1) / 2;
}
}
5_2
import java.util.scanner;
public class exercise02 {
public static void main(string[] args) {
scanner input = new scanner(system.in);
//prompt the user to enter an integer
system.out.print(enter an interger: );
long number = long();
system.out.println(the sum of the digits in + number + is + sumdigits(number)); }
public static int sumdigits(long n) {
int sum = 0;
long remainingn = n;
}
} do { long digit = remainingn % 10; remainingn = remainingn / 10; sum += digit; } while (remainingn != 0); return sum;
第03题
import java.util.scanner;
public class exercise03 {
public static void main(string[] args) {
scanner input = new scanner(system.in);
//prompt the user to enter an integer
system.out.print(enter an integer: );
int number = int();
//display result
system.out.println(is + number + a palindrome? + ispalindrome(number)); }
public static boolean ispalindrome(int number) {
if (number == reverse(number))
return true;
else
return false;
}
public static int reverse(int number) {
int reversenumber = 0;
do {
int digit = number % 10;
number = number / 10;
reversenumber = reversenumber * 10 + digit;
} while (number != 0);
return reversenumber;
}
第04题
import java.util.scanner;
public class exercise04 {
public static void main(string[] args) {
scanner input = new scanner(system.in);
//prompt the user to enter an integer
system.out.print(enter an integer: );
int number = int();
//display result
system.out.print(the reversal of + number + is );
reverse(number);
}
public static void reverse(int number) {
int reversenumber = 0;
do {
int digit = number % 10;
number = number / 10;
reversenumber = reversenumber * 10 + digit;
} while (number != 0);
system.out.println(reversenumber);
}
}
第05题
import java.util.scanner;
public class exercise05 {
public static void main(string[] args) {
scanner input = new scanner(system.in);
//prompt the user to enter three numbers
system.out.print(enter three numbers: );
double num1 = double();
}
double num3 = double(); system.out.print(num1 + + num2 + + num3 + in increasing order: ); displaysortednumbers(num1, num2, num3); } public static void displaysortednumbers(double num1, double num2, double num3) { double max = math.max(math.max(num1, num2), num3); double min = math.min(math.min(num1, num2), num3); double second = 0; if (num1 != max num1 != min)second = num1; if (num2 != max num2 != min)second = num2; if (num3 != max num3 != min)second = num3; system.out.println(min + + second + + max); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论