1. java 两道编程小题(赶)
import java.util.Scanner;
public class Rectangle{//长方体类
private double length;//长
private double width;//宽
public Rectangle(double length,double width){
this.setLength(length);
this.setWidth(width);
}
public double calculateArea(){//计算面积
return length*width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public static void main(String[] args){//测试方法
Scanner sc = new Scanner(System.in);
System.out.print("请输入土地的长度");
double length = sc.nextDouble();
System.out.print("请输入土地的宽度");
double width = sc.nextDouble();
Rectangle rec = new Rectangle(length,width);
System.out.println("您土地的面积为:"+rec.calculateArea());
System.out.print("请输入长方体的长度");
double len = sc.nextDouble();
System.out.print("请输入长方体的宽度");
double wid = sc.nextDouble();
System.out.print("请输入长方体的高度");
double hei = sc.nextDouble();
Cuboid cu = new Cuboid(len,wid,hei);
System.out.println("长方体的表面积为:"+cu.calCuboidArea());
System.out.println("长方体的体积为:"+cu.calculateVolume());
}
}
class Cuboid extends Rectangle{//长方体类
private double height;
public Cuboid(double length, double width,double height) {
super(length, width);
this.setHeight(height);
}
public double calculateVolume(){//计算体积
return super.calculateArea()*height;
}
public double calCuboidArea(){//计算表面积
double length = super.getLength();
double width = super.getWidth();
double area = (length*width+length*height+width+height)*2;
return area;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
**********************第二题****************************
import java.util.Scanner;
public class ComplexNumber {
protected double realPart;
protected double imaginPart;
public ComplexNumber() {
realPart = 0.0;
imaginPart = 0.0;
}
public ComplexNumber(double r, double i) {
realPart = r;
imaginPart = i;
}
public void setReal(double r) {
realPart = r;
}
public void setImagin(double i) {
imaginPart = i;
}
public double getReal() {
return realPart;
}
public double getImagin() {
return imaginPart;
}
public String toString() { //重写toString方法,方便测试
return String.valueOf(realPart + " + " + imaginPart + "i");
}
public static ComplexNumber complexAdd(ComplexNumber num1,ComplexNumber num2) {//复数相加
ComplexNumber result = new ComplexNumber();
result.setReal(num1.getReal() + num2.getReal());
result.setImagin(num1.getImagin() + num2.getImagin());
return result;
}
public static ComplexNumber complexMinus(ComplexNumber num1,ComplexNumber num2) {//复数相减num1-num2
ComplexNumber result = new ComplexNumber();
result.setReal(num1.getReal() - num2.getReal());
result.setImagin(num1.getImagin() - num2.getImagin());
return result;
}
public static ComplexNumber complexMulti(ComplexNumber num1,ComplexNumber num2) {//复数相乘
ComplexNumber result = new ComplexNumber();
double realTemp = 0.0;
double imaginTemp = 0.0;
realTemp = num1.getReal() * num2.getReal() - num1.getImagin()* num2.getImagin();
imaginTemp = num1.getReal() * num2.getImagin() + num1.getImagin()* num2.getReal();
result.setReal(realTemp);
result.setImagin(imaginTemp);
return result;
}
public static ComplexNumber complexDiv(ComplexNumber num1,ComplexNumber num2) {//复数相除num1/num2
ComplexNumber result = new ComplexNumber();
double realTemp = 0.0;
double imaginTemp = 0.0;
double div = 0.0;
div = Math.pow(num2.getReal(), 2) + Math.pow(num2.getImagin(), 2);
realTemp = num1.getReal() * num2.getReal() + num1.getImagin()* num2.getImagin();
imaginTemp = num2.getReal() * num1.getImagin() - num1.getReal()* num2.getImagin();
result.setReal(realTemp / div);
result.setImagin(imaginTemp / div);
return result;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入复数num1的实数部分");
double real1 = sc.nextDouble();
System.out.print("请输入复数num1的虚数部分");
double imagin1 = sc.nextDouble();
ComplexNumber num1 = new ComplexNumber(real1,imagin1);
System.out.println("实数num1为:"+num1.toString());
System.out.print("请输入复数num2的实数部分");
double real2 = sc.nextDouble();
System.out.print("请输入复数num2的虚数部分");
double imagin2 = sc.nextDouble();
ComplexNumber num2 = new ComplexNumber(real2,imagin2);
System.out.println("实数num2为:"+num1);
System.out.println("num1 + num2 = "+ComplexNumber.complexAdd(num1, num2));
System.out.println("num1 - num2 = "+ComplexNumber.complexMinus(num1, num2));
System.out.println("num1 * num2 = "+ComplexNumber.complexMulti(num1, num2));
System.out.println("num1 / num2 = "+ComplexNumber.complexDiv(num1, num2));
}
}
2. JAVA编程题目
从面向对象的角度分析,下列四组事物中,哪一组属于类—对象模式? (D )
下列哪一个不属于版面向权对象语言的重要特性 (C )
Java application中的主类需包含main方法,main方法的返回类型是什么?(D)
在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。这种面向对象程序的特性称为( C)。
以下代码段执行后的输出结果为(C )
编译运行以下程序后,关于输出结果的说明正确的是 (B)
3. JAVA考试题
1、请问 ”2” 、’2’、2之间有什么不同?并回答下面程序的输出结果是什么? (提示:‘2’的ASCII码值是50 )(8分)
答:"2"是字符串,'2'算字符。2是数字。
class test
{
public static void main(String[] args)
{
int a=2;
int b='2';
System.out.println (a+a);
System.out.println (a+b);
}
} 输出结果为:4
52
2、你认为java、C、C++他们之间有没有联系和区别?和C、C++相比,java有哪些优点?(10分)
答:java是以c及c++为基础的。许多地方沿用了它们的思想。但最主要的,java是完全面向对象的编程,而c是面向过程,c+则不完全是面向对象。java相对说来,编程更方便,安全,结构,模块化强,易于移植,跨平台性好等。
3、下面两段代码具有多处错误,请找出你认为错误的地方,作上标记,并说明为何出错。如果你认为该行没有错误,请打上√ (12分)
i)public int search (int 错[10] number) 1、 引用时只能是类型不能带值{
number错 = new int[10]; 2、 数组没有下标
for (int i=0;i<number.length;i++) 3、对
{
number[i]=number[i-1]+number[i+1]错; 4、数组在i+1在i=number.length-1是超界
return number; 5、对
}
}
ii)
class MyclassOne
{
public final int A=365;
public int b;
private float c;
private void myMethodOne(int a)
{
b=a;
}
public float myMethodTwo()
{
return 36;
}
}
class MyClassMain
{
public static void main(String[] args)
{
MyClassOne w1=new MyClassOne();
w1.A=12; 6、错误,试图给final型再次赋值
w1.b=12; 7、对Myclassone中b赋值
w1.c=12; 8、对myclassone float c赋值
w1.myMethodOne(12); 9、调用myclassone的mymethodone形参为int的方法,
w1.myMethodOne(); 10、调用myclassone的mymethodone无形参的方法System.out.println(w1.myMethodTwo(12)); 11、输出myclassone的mymethodtwo(12)值
w1.c=w1.myMethodTwo(); 12让c引用mymethodtwo方法
}
}
请简要说明下面程序的功能
1) public class Sum ( 5分 )
{ public static void main( String args[ ])
{ double sum = 0.0 ;
for ( int i = 1 ; i <= 100 ; i + + )
sum += 1.0/(double) i ;
System.out.println( "sum="+sum );
}
} 功能为 求出1/1+1/2+1/3+1/4….+1/100的和
程序设计:(10分)
编写一个java程序。要求该程序能够具有以下功能:
定义一个坐标类coord。坐标类coord必须满足如下要求:
a)coord类含有两部分数据:横坐标x和纵坐标y。x和y的类型都是int类型。
b)coord类的方法有:
coord( ) : 构造函数,将横坐标和纵坐标的值都赋值为0
coord( int x , int y ) : 构造函数,形参 x 为横坐标的初值,y为纵坐标的初值。
coord coordAdd(int x, int y) : 将当前坐标对象与形参的值相加,所得的结果仍是一个坐标,返回给此方法的调用者。
(提示:可以将两个坐标相加定义为横坐标和横坐标相加,纵坐标和纵坐标相加。例如(1,2)+(3,4)=((1+3),(2+4))=(4,6))
public class Coord {
int x=100;
int y=200;
public Coord(){
this.x=0;this.y=0;
}
public Coord(int x,int y) {
x=this.x;y=this.y;
}
void coordAdd(int x,int y){
this.x+=x;
this.y+=y;
}
public static void main (String[] args) {
}
}
程序设计:(10分)
请编写一个java程序,利用该程序计算并输出 1+2+3+……+100的值
Sum.java
public class Sum {
public Sum() {
int sum=0;
for(int i=1;i<=100;i++)
sum+=i;
System.out.println("1+2+3+...100="+sum);
}
public static void main (String[] args) {
new Sum();
}
}
OK??
4. Java编程题
第二个:
package test;
import java.util.Arrays;
import java.util.List;
public class TestArray {
public static void main(String[] args) {
Integer arr[] = { 10,7,6,9,8,2};
System.out.println(arrayContains(arr, 1));
System.out.println(arrayContains1(arr, 2));
}
public static boolean arrayContains(Integer[] arr,int targetValue){
List<Integer> list = Arrays.asList(arr);
return list.contains(targetValue);
}
public static int arrayContains1(Integer[] arr,int targetValue){
List<Integer> list = Arrays.asList(arr);
if(!list.contains(targetValue)){
return -1;
}else{
for (int index = 0; index < list.size(); index++) {
Integer val = list.get(index);
if(val==targetValue){
return index;
}
}
}
return -1;
}
}