一道JAVA编程题:用类描述汽车中Engine(发动机)的功率和Gearbox(变速箱)的档位数目
--------Engine 类----------
public class Engine {
int power;
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
}
----------Gearbox类---------
public class Gearbox {
int amount;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
-------------Car 类---------------
public class Car {
Engine engine = new Engine();
Gearbox gearbox = new Gearbox();
public Engine getEngine() {
return engine;
}
public void setEngine(Engine c) {
this.engine = c;
}
public Gearbox getGearbox() {
return gearbox;
}
public void setGearbox(Gearbox h) {
this.gearbox = h;
}
public void show(){
int power = engine.getPower();
int amount = gearbox.getAmount();
System.out.println("功率为:"+power+" 变速箱:"+amount);
}
}
---------------test 类--------------------
public class test {
public static void main(String[] args) {
Engine engine = new Engine();
engine.setPower(123);
Gearbox gearbox = new Gearbox();
gearbox.setAmount(6);
Car jeep = new Car();
jeep.setEngine(engine);
jeep.setGearbox(gearbox);
jeep.show();
}
}
这种题太没32313133353236313431303231363533e59b9ee7ad9431333335346139营养了...看你应该是个初学者,又在线等,帮你一把.
JAVA编程题
//圆类Circle
import java.util.Scanner;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造函数
public Circle(){
this.radius=0;
}
//带参构造函数
public Circle(double r ){
this.radius=r;
}
//获取面积
public double getArea(){
double r=this.radius;
double area=r*r*3.14;
return area;
}
//获取周长
public double getPerimeter(){
double perimeter=this.radius*2*3.14;
return perimeter;
}
//打印圆的信息
public void show(){
System.out.println("请输入圆的半径");
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println("圆的半径"+this.getRadius());
System.out.println("圆的面积"+this.getArea());
System.out.println("圆的周长"+this.getPerimeter());
}
}
//圆柱类
public class Cylinder extends Circle {
//圆柱高
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//构造方法
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println("圆柱的体积"+this.getVolume());
}
}
//主程序入口
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();
}
}
PS:注释写的很详尽了,求采纳
JAVA编程题求全部代码
class HW1 {
public static void main(String[] args) {
double[] test = new double[]{5.2, 1.0, 6.7, 3.4, 100.5, 55.5};
BoundryValues boundryValues = getBoundryValues(test);
System.out.println("Min Value = " + boundryValues.getMin());
System.out.println("Max Value = " + boundryValues.getMax());
System.out.println("Ave Value = " + boundryValues.getAve());
}
private static class BoundryValues {
private double max;
private double min;
private double ave;
public BoundryValues(){}
public BoundryValues(double max, double min, double ave) {
this.max = max;
this.min = min;
this.ave = ave;
}
public void setMax(double max) {
this.max = max;
}
public double getMax() {
return max;
}
public void setMin(double min) {
this.min = min;
}
public double getMin() {
return min;
}
public void setAve(double ave) {
this.ave = ave;
}
public double getAve() {
return ave;
}
}
public static BoundryValues getBoundryValues(double[] doubles) {
BoundryValues boundryValues = new BoundryValues();
double[] results = sort(doubles);
double total = 0.0;
for (int i = 0; i < results.length; i ++) {
total += results[i];
}
boundryValues.setMin(results[0]);
boundryValues.setMax(results[results.length - 1]);
boundryValues.setAve(total/results.length);
return boundryValues;
}
public static double[] sort(double[] doubles) {
for (int i = 0; i < doubles.length; i ++) {
for (int j = 0; j < doubles.length - i - 1; j ++) {
if (doubles[j] > doubles[j + 1]) {
double temp = doubles[j];
doubles[j] = doubles[j + 1];
doubles[j + 1] = temp;
}
}
}
return doubles;
}
}import java.util.*;
class HW2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a, b, c;
System.out.println("Enter a, b, c:");
a = scanner.nextDouble();
b = scanner.nextDouble();
c = scanner.nextDouble();
Setsets = calculate(a, b, c); for (Double d : sets) { System.out.println("Values are: " + d + "
"); } } public static Set calculate(double a, double b, double c) { Set sets = new HashSet(); if (Math.pow(b, 2.0) - 4 * a * c < 0) { System.err.println("No value"); } else { sets.add((- b + Math.sqrt(Math.pow(b, 2.0) - 4 * a * c)) / 2.0 * a); sets.add((- b - Math.sqrt(Math.pow(b, 2.0) - 4 * a * c)) / 2.0 * a); } return sets; } }下午接着写
java编程题:随机产生20个50~100之间的整数,输出这20个数并找出最大数和最小数。
public class P1 {
private int[] num=new int[20];
private int Maxnum,Minnum;
public P1(){
for(int i =0;i<20;i++){
num[i]=50+(int)(Math.random()*50);
}
}
public void print(){
for(int j=0;j<20;j++){
System.out.print(num[j]+" ");
}
System.out.println();
}
public void setMaxAndMin(){
Maxnum=Minnum=num[0];
for(int k=1;k<20;k++){
if(Maxnum<num[k]){
Maxnum=num[k];
}else if(Minnum>num[k]){
Minnum=num[k];
}
}
}
public int getMax(){
return Maxnum;
}
public int getMin(){
return Minnum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
P1 p = new P1();
p.print();
p.setMaxAndMin();
System.out.println("最大值为:"+p.getMax());
System.out.println("最小值为:"+p.getMin());
}
}
一个JAVA编程题 用以计算y年m1月d1日与同年的m2月d2之间的天数(m2>=m1),并打印计算结果。.若m1=m2且d1=d2则算1天.在闰年时,2月有29天.闰年的年号由以下方式确定:该年号能被4整除但不能被100整除,或者该年号能被400整除则是闰年.例:输入1997,
Calendar类,你最好的选择,假如不允许使用这些类库的话,只让很基础的做的话,只需要很多个if语句罢了 public class Calc { private int year; private int startMonth; private int startDay; private int endMonth; private int endDay; public Calc(int year,int startMonth,int startDay,int endMonth,int endDay){ this.year = year; this.startMonth = startMonth; this.startDay = startDay; this.endMonth = endMonth; this.endDay = endDay; } public int calcDay() throws Exception{ if(!validate()){ throw new Exception("麻烦别输入错误的日期"); } int day = 0; for(int i = startMonth+1;i<endMonth;i++){ day+= getMonthDay(i); } day+=endDay; day+=(getMonthDay(startMonth)-startDay+1); return day; } /** * 校验数据是否准确,自己写 * @return */ public boolean validate(){ return true; } public int getMonthDay(int month) throws Exception{ if(month==2){ if((year%4==0&&year%100!=0)||year%400==0){ return 29; }else{ return 28; } }else if(month==1){ return 31; }else if(month==3){ return 31; }else if(month==4){ return 30; }else if(month==5){ return 31; }else if(month==6){ return 30; }else if(month==7){ return 31; }else if(month==8){ return 31; }else if(month==9){ return 30; }else if(month==10){ return 31; }else if(month==11){ return 30; }else if(month==12){ return 31; }else{ throw new Exception("You input wrong month!"); } } public static void main(String[] args) throws Exception { Calc c = new Calc(1997, 2, 27, 3, 10); System.err.println(c.calcDay()); } }
求解JAVA编程题:编写一个应用程序,创建三个线程分别显示各自的运行时间
import java.util.Calendar; import java.util.concurrent.locks.Lock; public class MultiThread { static Lock mylock; public static void main(String[] args) { RunningObject1 r1 = new RunningObject1(); Thread t1 = new Thread(r1, "t1"); Thread t2 = new Thread(r1, "t2"); Thread t3 = new Thread(r1, "t3"); t1.start(); t3.start(); t2.start(); } static class RunningObject1 implements Runnable { public void run() { synchronized(this){ String name=Thread.currentThread().getName(); System.out.println(name+"开始时间:"+Calendar.getInstance().getTimeInMillis()); for (int i = 0; i < 100000000; i++) { if (i == 9999999) { System.out.println(name+"结束时间:"+Calendar.getInstance().getTimeInMillis()); break; } } } } } }
JAVA编程题(String)
package com.zzb.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public boolean isNum(int c) {
return 48 <= c && 57 >= c;
}
//通过比较ASCII码判断是否符合要求
public boolean test(String s) {
char[] ss = s.toCharArray();
if (ss.length != 11)
return false;
int i = 0;
for (int j = 0; j < 3; j++) {
if (!isNum(ss[i]))
return false;
i++;
}
if (ss[i] != -)
return false;
i++;
for (int j = 0; j < 2; j++) {
if (!isNum(ss[i]))
return false;
i++;
}
if (ss[i] != -)
return false;
i++;
for (int j = 0; j < 4; j++) {
if (!isNum(ss[i]))
return false;
i++;
}
return true;
}
//正则表达式法
public boolean test2(String s){
Pattern pat = Pattern.compile("[\d]{3}-[\d]{2}-[\d]{4}");
Matcher mat = pat.matcher(s);
return mat.matches();
}
public static void main(String[] args) {
Test test = new Test();
String s = "111-11-1111";
System.out.println(test.test(s));
System.out.println(test.test2(s));
s = "111-11-111x";
System.out.println(test.test(s));
System.out.println(test.test2(s));
s = "111-11-11111";
System.out.println(test.test(s));
System.out.println(test.test2(s));
s = "111-11X1111";
System.out.println(test.test(s));
System.out.println(test.test2(s));
}
}
java编程题:求所有两位数内能被3整除的和及不能被三整除的数的和~拜托了
var i,j:integer;
j:=0;
for i=10 to 99 do
begin
if i mod 3 = 0 then j=j+1;
end;
showmessage(能被3整除的两位数有+inttostr(j)+个,不能被3整除的两位数有+inttostr(100-j)+个);
java编程题 谢谢了
你这问题问了两遍, 我把我刚回答的给你粘过来: 比如该用户是User对象 user 积分增加之前获取该客户的当前积分和将要增加的积分,以及客户类型,如0代表普卡客户,1代表金卡客户,如: double current_point = user.getPoint(); double point_add = xxxx; int cust_type = user.getCust_type(); 判断: user.setPoint(current_point + point_add); if(cust_type == 0){ if (current_point <= 5000 && (current_point + point_add ) > 5000){ user.setPoint(current_point + point_add + 500); } }else if(cust_type == 1){ if (current_point <= 1000 && (current_point + point_add ) > 1000){ user.setPoint(current_point + point_add + 500); } }else{ //返回错误信息:用户类型不存在 } 这只是个大体逻辑,现实应用中还有很多其他方面的细节,仅供参考