重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Arrays;
定南网站建设公司成都创新互联,定南网站设计制作,有大型网站制作公司丰富经验。已为定南近千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的定南做网站的公司定做!
public class Array2 {
public static void main(String[] args) {
//声明一个名为myArray的数组,该数组有2行,每行列数不等,并为其分配内存空间
int[][] myArray = new int[2][];
myArray[0] = new int[5]; //第一行有5个元素,并为其分配内存空间
myArray[1] = new int[10]; //第二行有10个元素,并为其分配内存空间
for (int j = 0; j myArray[0].length; j++)
//用1-10之间的随机整数给第一行元素赋值
myArray[0][j] = (int)(Math.random() * 10);
//用100-200之间的随机整数给第二行元素赋值
for (int j=0; j myArray[1].length; j++)
myArray[1][j]=(int)(Math.random() * 100 + 100);
for (int i=0; i myArray.length; i++){ //输出myArray数组各元素的值
for (int j=0; j myArray[i].length; j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
Arrays.sort(myArray[0]); //对第一行元素排序
Arrays.sort(myArray[1]); //对第二行元素排序
System.out.println("\n排序后的数组元素: ");
for (int i=0; imyArray.length;i++){ //再次输出myArray数组各元素的值
for (int j=0; jmyArray[i].length;j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
}
}
7 3 9 6 7
103 165 166 148 103 179 128 109 120 156
排序后的数组元素:
3 6 7 7 9
103 103 109 120 128 148 156 165 166 179
A:
public void frighten() {
System.out.println("Arrrgh!");
}
B:
public void frighten() {
System.out.println("A bite?");
}
要问为什么,懒得说,仔细想一下类的继承和多态.
package xinguan;
abstract class Operation{ //抽象类
public static double numberA= 0;
public static double numberB = 0;
abstract double getResult(); //抽象方法
}
class OperationADD extends Operation{
@Override
double getResult() {
return numberA+numberB;
}
}
class OperationSUB extends Operation{
@Override
double getResult() {
return numberA-numberB;
}
}
class OperationMUL extends Operation{
@Override
double getResult() {
return numberA*numberB;
}
}
class OperationDIV extends Operation{
@Override
double getResult() {
return numberA/numberB;
}
}
class OperationFactory{
public static Operation createOperate(char operate){
Operation oper = null;
switch (operate){
case'+':
oper= new OperationADD();
break;
case'-':
oper= new OperationSUB();
break;
case'*':
oper= new OperationMUL();
break;
case'/':
oper= new OperationDIV();
break;
}
return oper;
}
}
public class CalculateDemo {
/**
* @param args
*/
public static void main(String[] args) {
Operation operADD = OperationFactory.createOperate('+');
Operation operSUB = OperationFactory.createOperate('-');
Operation operMUL = OperationFactory.createOperate('*');
Operation operDIV = OperationFactory.createOperate('/');
operADD.numberA = 15.0;
operADD.numberB = 3;
System.out.println(operADD.getResult());
System.out.println(operSUB.getResult());
System.out.println(operMUL.getResult());
System.out.println(operDIV.getResult());
}
}
因为抽象类是静态方法 所以 给operADD 那个对象赋值一次就能获得所有结果。要是去掉static 那么就需要每个对象 赋值。现在基本满足你的要求了。
class Father {
private int f1, f2;
public Father(int x) {
f1 = x;
}
public Father(int x, int y) {
// 和上面一个参数的构造方法做同样的操作,包括赋值 f1=x
f1 = x ;
f2 = y;
}
public void display() {
System.out.println("f1=" + f1 + "f2=" + f2);
}
}
class Son extends Father { // 继承类 Father
private int s1;
private int s2;
public Son(int x, int y) {
// 调用父类的构造方法,传递参数x
super(x);
s1 = y;
}
public void display() {
// 调用父类的display()方法
super.display();
System.out.println("s1=" + s1 + "s2=" + s2);
}
}
public class Test {
public static void main(String args[]) {
Father f = new Son(5, 6);
f.display();
}
}
以下代码仅供参考,用的是另外一种思路
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
SimpleDateFormat s = new SimpleDateFormat("yyyy");
int year = Integer.parseInt(s.format(new Date()));
s = new SimpleDateFormat("MM");
int month = Integer.parseInt(s.format(new Date()));
s = new SimpleDateFormat("dd");
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
System.out.println(year + "年" + month + "月" + day + "日" + "\t" + "日历如下:");
c = Calendar.getInstance();
int currentMonth = -1;
c.set(year, 0, 1);
while (c.get(Calendar.YEAR) == year) {
/**
* 判断是否处于同一个月份
* 若不是 则需要输出表头
*
*/
if (currentMonth != c.get(Calendar.MONTH)) {
System.out.println("");
System.out.println("");
currentMonth = c.get(Calendar.MONTH);
System.out.println((currentMonth + 1) + "月");
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int i = 1, k = c.get(Calendar.DAY_OF_WEEK); i k; i++) {
System.out.print(String.format("%1$5s\t", ""));
}
}
System.out.print(String.format("%1$5d\t", c.get(Calendar.DAY_OF_MONTH)));
/**
* 日期+1
*/
c.add(Calendar.DAY_OF_YEAR, 1);
/**
* 当日期是星期天并且不是一个月的最后一天时 输出换行
*/
if (c.get(Calendar.DAY_OF_WEEK) == 1 currentMonth == c.get(Calendar.MONTH)) {
System.out.println("");
}
}
}
}
楼主,JAVA类的首字母大字的。。。。。。
继承就是使用extends
class Bmw extends Car{
public String getInfo(){
return "I am BMW.";
}
}
出题出得不好,事实上,往往在覆盖toString中输出信息,实用、不啰嗦。