java编程基础答案
package mytest;
interface Number
{
int compare(Number n);
double getValue();
String toString();
}
class Fraction implements Number
{
int n1;//分子
int n2;//分母
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public String toString()
{
System.out.println(this.getValue());
return "";
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}
@Override
public double getValue() {
double result = 0.0;
if(n2 != 0)
{
result = (double)n1 / n2;
}
return result;
}
// public String toString()
// {
//
// }
}
class Complex implements Number{
int n1;//实部
int n2;//虚部
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}
public String toString()
{
System.out.println(this.getValue());
return "";
}
@Override
public double getValue() {
return Math.pow((Math.pow(n1, 2)+Math.pow(n2, 2)), 0.5);
}
}
public class Jxxxx {
public static void main(String[] args)
{
//测试分数
Fraction f1 = new Fraction();
f1.setN1(3);
f1.setN2(5);
Fraction f2 = new Fraction();
f2.setN1(1);
f2.setN2(2);
double val1 = f1.getValue();
double val2 = f2.getValue();
f1.toString();
f2.toString();
int result = f1.compare(f2);
System.out.println(result);
//测试复数
Complex c1 = new Complex();
c1.setN1(3);
c1.setN2(4);
Complex c2 = new Complex();
c2.setN1(5);
c2.setN2(12);
double v1 = c1.getValue();
double v2 = c2.getValue();
c1.toString();
c2.toString();
int res = c1.compare(c2);
System.out.println(res);
System.out.println(c1.toString());
}
}
B. 《Java语言程序设计基础篇》第六版的练习题和编程题答案
哥们我给你写完了,耽误了我半个小时的时间啊!你直接运行就可以了
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Constellation implements ActionListener{
private JFrame frame = null;
private JTextField year = null;
private JTextField month = null;
private JTextField day = null;
private JLabel label1 = null;
private JLabel label2 = null;
private JLabel label3 = null;
private JPanel panel1 = null;
private JPanel panel2 = null;
private JButton button = null;
private JTextField output = null;
public static final String[] zodiacArr = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇",
"马", "羊" };
public static final String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座",
"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22,
22 };
/**
* * 根据日期获取生肖 *
* @return 11.
*/
public static String date2Zodica(Calendar time) {
return zodiacArr[time.get(Calendar.YEAR) % 12];
}
/**
* * 根据日期获取星座 *
* @param time *
* @return
*/
public static String date2Constellation(Calendar time) {
int month = time.get(Calendar.MONTH);
int day = time.get(Calendar.DAY_OF_MONTH);
if (day < constellationEdgeDay[month]) {
month = month - 1;
}
if (month >= 0) {
return constellationArr[month];
}
// default to return 魔羯
return constellationArr[11];
}
public Constellation(){
frame = new JFrame("计算生肖,星座");
year = new JTextField("",3);
month = new JTextField("",3);
day = new JTextField("",3);
label1 = new JLabel("请输入年份:");
label2 = new JLabel(",请输入月份:");
label3 = new JLabel(",请输入日期:");
button = new JButton("查看结果");
button.addActionListener(this);
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
panel1.add(label1);
panel1.add(year);
panel1.add(label2);
panel1.add(month);
panel1.add(label3);
panel1.add(day);
panel1.add(button);
frame.setLayout(new BorderLayout());
frame.add(panel1,BorderLayout.NORTH);
panel2 = new JPanel();
output = new JTextField("",40);
panel2.add(output,JPanel.CENTER_ALIGNMENT);
frame.add(panel2,BorderLayout.CENTER);
frame.setSize(500, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
output.setText("");
int y = Integer.parseInt(year.getText());
int m = Integer.parseInt(month.getText());
int d = Integer.parseInt(day.getText());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, y);
calendar.set(Calendar.MONTH, m);
calendar.set(Calendar.DAY_OF_MONTH, d);
String zodica = date2Zodica(calendar);
String constellation = date2Constellation(calendar);
String str = "您输入的日期为:"+y+"年-"+m+"-月"+d+"日,得到的生肖:"+zodica+",星座:"+constellation;
output.setText(str);
}
//testcode
public static void main(String[] args) {
new Constellation();
}
}
C. 谁知道java编程基础应用与实例课后习题答案在那下载
哥们,不妨到你们的学校图书馆看一下,我们当时上学的用的那个Java课本,我们学校图书馆有专门和之对应的课后习题解答的书。
D. 求一题简单的java编程题答案
public class Test {
public static void main(String[] args) {
int sum = 0, j;
boolean isPrime = true;
for (int i = 2; i <= 20; i++) {
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
sum += i;
} else {
isPrime = true;
}
}
try {
sum = sum / 0;
} catch (Exception e) {
System.out.println("和为:"+sum+",且除数不能为0");
}
}
}
E. java编程作业,求答案
import java.awt.*;
import java.awt.event.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("姓名:xxx");
frame.setLayout(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel();
TextField tf = new TextField(20);
panel1.add(tf);
panel2.setLayout(new GridLayout(4,4));
panel2.add(new Button("0"));
panel2.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel2.add(new Button("4"));
panel2.add(new Button("5"));
panel2.add(new Button("6"));
panel2.add(new Button("7"));
panel2.add(new Button("8"));
panel2.add(new Button("9"));
panel2.add(new Button("A"));
panel2.add(new Button("B"));
panel2.add(new Button("C"));
panel2.add(new Button("D"));
panel2.add(new Button("E"));
panel2.add(new Button("F"));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
F. 求Java编程的答案
importjava.util.Arrays;
classStudent{
privateStringname;
privateintage;
privateStringsex;
privatedoubleexamDesign;
privatedoubleexamDB;
privatedoubleexamXML;
publicStudent(){
}
publicStudent(Stringname,intage,booleansex){
}
publicStudent(Stringname,intage,Stringsex,doubleexamDesign,
doubleexamDB,doubleexamXML){
this.name=name;
this.age=age;
this.sex=sex;
this.examDesign=examDesign;
this.examDB=examDB;
this.examXML=examXML;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicdoublegetExamDesign(){
returnexamDesign;
}
publicvoidsetExamDesign(doubleexamDesign){
this.examDesign=examDesign;
}
publicdoublegetExamDB(){
returnexamDB;
}
publicvoidsetExamDB(doubleexamDB){
this.examDB=examDB;
}
publicdoublegetExamXML(){
returnexamXML;
}
publicvoidsetExamXML(doubleexamXML){
this.examXML=examXML;
}
publicStringsubtotal(){
return"总成绩:"+(examDesign+examDB+examXML);
}
publicStringavgScore(){
return"平均分:"+(examDesign+examDB+examXML)/3;
}
publicStringtopScore(){
double[]arr={examDesign,examDB,examXML};
Arrays.sort(arr);
return"最高分:"+arr[2];
}
publicStringbottomScore(){
double[]arr={examDesign,examDB,examXML};
Arrays.sort(arr);
return"最低分:"+arr[0];
}
@Override
publicStringtoString(){
returnname+"的年龄是"+age+",性别是"+sex+",程序设计成绩是"+examDesign
+",数据库成绩是"+examDB+",XML成绩是"+examXML+"]";
}
}
publicclassTestStudent{
publicstaticvoidmain(String[]args){
Students=newStudent("小明",22,"男",87,88,89);
System.out.println(s.avgScore());
System.out.println(s.bottomScore());
System.out.println(s.topScore());
System.out.println(s.subtotal());
System.out.println(s);
}
}
G. java编程解答
第一个:
publicclassApp1{
staticintfib(intn){
if(n==0||n==1){
return1;
}
returnfib(n-1)+fib(n-2);
}
publicstaticvoidmain(String[]args){
for(inti=20;i<=30;i++){
System.out.print(Integer.toString(fib(i))+"");
}
}
}
第二个:
publicclassApp2{
publicstaticvoidmain(String[]args){
for(inti=0;i<=20;i++){
for(intj=0;j<=33;j++){
for(intk=0;k<=100;k++){
if(i*5+j*3+k/3==100&&i+j+k==100&&k%3==0)
System.out.println("公鸡:"+i+"母鸡:"+j+"小鸡:"+k);
}
}
}
}
}
第三个:
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.*;
classBook{
privateStringtitle;
privateStringauthor;
privateintpageCount;
publicBook(){
this("","",0);
}
publicBook(Stringtitle,Stringauthor,intpageCount){
this.title=title;
this.author=author;
this.pageCount=pageCount;
}
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicStringgetAuthor(){
returnauthor;
}
publicvoidsetAuthor(Stringauthor){
this.author=author;
}
publicintgetPageCount(){
returnpageCount;
}
publicvoidsetPageCount(intpageCount){
this.pageCount=pageCount;
}
publicvoidprint(){
System.out.println(title+" "+author+" "+pageCount);
}
publicStringgetBookInfo(){
return"书名:"+title+",作者:"+author+",页数:"+pageCount;
}
}
{
privatestaticScannerscanner=newScanner(System.in);
privatestaticList<Book>books=newArrayList<Book>();
privatestaticBookinput(){
Bookbook=newBook();
System.out.print("请输入书名:");
book.setTitle(scanner.next());
System.out.print("请输入作者:");
book.setAuthor(scanner.next());
System.out.print("请输入页数:");
book.setPageCount(scanner.nextInt());
returnbook;
}
publicstaticvoidmain(String[]args)throwsIOException{
while(true){
System.out.print("是否添加图书?(true/false)");
if(!scanner.nextBoolean()){
break;
}
Bookbook=input();
books.add(book);
}
FileWriterwriter=newFileWriter("books.txt");
for(Bookbook:books){
writer.write(book.getBookInfo()+System.lineSeparator());
}
writer.close();
}
}
H. java编程基础
这三段的区别就是if else语句的逻辑区别。
第一段相当于
if(x>0){
max=mid-1;
}
if(x<0){
min=mid+1;
}else//这个else是和上面的if(x<0)匹配的,相当于if(x>=0),但应该是等于0才算找到,所以就错了
{
returnmid;
}
第二段三个if语句独立,且条件都不相交,所以不会干扰。
第三段
if(x>0)
max = mid - 1;
else if(x<0)
min = mid + 1;
else // 这里三个条件语句是一个整体,这个else是前两个条件都不满足的情况,不大于又不小于,就是等于了
return mid;