本文目录一览:
- 1、求java基础编程和运行结果截图,程序代码最好做详细解释,万分感激
- 2、java编程问题(请直接给代码)
- 3、java编程,用代码实现计算器类(Calculator)
- 4、java代码解析
- 5、一道java编程题
求java基础编程和运行结果截图,程序代码最好做详细解释,万分感激
public class Test {//测试类
public static void main(String[] args) {
Student[] stus = new Student[5];
Student stu1 = new Student();
stu1.setAge(11);
stu1.setName("Zhang San");
stus[0] = stu1;
Student stu2 = new Student();
stu2.setAge(22);
stu2.setName("Li Si");
stus[1] = stu2;
Postgraduate p1 = new Postgraduate();
p1.setAge(33);
p1.setName("Wang Wu");
p1.setStudydirection("Computer");
stus[2] = p1;
Postgraduate p2 = new Postgraduate();
p2.setAge(44);
p2.setName("Zhao Liu");
p2.setStudydirection("English");
stus[3] = p1;
Undergraduate u1 = new Undergraduate();
u1.setAge(55);
u1.setName("Ma qi");
u1.setSpecialty("Maths");
stus[4] = u1;
for(int i = 0; i stus.length; i++){
stus[i].print();
System.out.println();
}
}
}
class Student {//学生类
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void print(){
System.out.println("name: " + name);
System.out.println("Age: " + age);
}
}
class Undergraduate extends Student {//本科生类
private String specialty;
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public void print(){
super.print();
System.out.println("specialty : " + specialty);
}
}
class Postgraduate extends Student {//研究生类Postgraduate
private String studydirection;
public String getStudydirection() {
return studydirection;
}
public void setStudydirection(String studydirection) {
this.studydirection = studydirection;
}
public void print(){
super.print();
System.out.println("studydirection: " + studydirection);
}
}
----------------
name: Zhang San
Age: 11
name: Li Si
Age: 22
name: Wang Wu
Age: 33
studydirection: Computer
name: Wang Wu
Age: 33
studydirection: Computer
name: Ma qi
Age: 55
specialty : Maths
java编程问题(请直接给代码)
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher("Wang Laoshi", false, 19910123, "qinghua", 3);
System.out.println(teacher.toString());
CollegeStudent collStu = new CollegeStudent("College Student", true, 19851231, "Beijingdaxue","001", 2, "Computer", 3);
System.out.println(collStu.toString());
MiddleSchStudent middStu = new MiddleSchStudent("Middle schoole student", false, 19910705, "Di yi zhongxue", "002", 17, "English", 5);
System.out.println(middStu.toString());
}
}
abstract class Person {
protected String name;
protected boolean sex;
protected int birthday;
public Person(){}
public Person(String name, boolean sex, int birthday){
this.name =name;
this.sex = sex;
this.birthday = birthday;
}
public String toString(){
return "name:" + name + ", sex:" + sex + ", birthday: " + birthday;
}
}
class Teacher extends Person{
private String school;
private int schoolType;
public Teacher(){
}
public Teacher(String name, boolean sex, int birthday, String school, int schoolType){
super(name, sex, birthday);
this.school = school;
this.schoolType = schoolType;
}
public String toString(){
return super.toString() + ", school: " + school + ", school type: " + schoolType;
}
}
abstract class Student extends Person{
protected String school;
protected String no;
protected int grade;
public Student(){
}
public Student(String name, boolean sex, int birthday, String school, String no, int grade){
super(name, sex, birthday);
this.school = school;
this.no = no;
this.grade = grade;
}
public String toString(){
return super.toString() + ", school:" + school + ", student no: " + no + ", studeng grade: " + grade;
}
}
class CollegeStudent extends Student{
private String major;
private int grade;
public CollegeStudent(){
}
public CollegeStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
this.major = major;
this.grade = colledgeGrade;
}
public String toString(){
return super.toString() + ", major:" + major + ", grade: " + grade;
}
}
class MiddleSchStudent extends Student{
private int grade;
public MiddleSchStudent(){
}
public MiddleSchStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
this.grade = colledgeGrade;
}
public String toString(){
return super.toString() + ", grade: " + grade;
}
}
---------------测试
name:Wang Laoshi, sex:false, birthday: 19910123, school: qinghua, school type: 3
name:College Student, sex:true, birthday: 19851231, school:Beijingdaxue, student no: 001, studeng grade: 2, major:Computer, grade: 3
name:Middle schoole student, sex:false, birthday: 19910705, school:Di yi zhongxue, student no: 002, studeng grade: 17, grade: 5
java编程,用代码实现计算器类(Calculator)
public class Calculator {
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
java代码解析
一楼的说的够全面了,不过稍有误解.
再来表示抱歉,我对编程语言中的中文名词非常不了解,所以如果以下的回复对你的阅读或者理解造成困难,请见谅.
1.首先,要明白这个问题的答案,需要了解call (pass) by value 和 call (pass) by reference 的区别.简单来说:
call by value通常是复制这个parameter的值去另外一块内存里,然后传给function, 所以在method/function里边对这个变量的所有变更,实际上都是对复制过来的镜像进行操作,不会对原本的variable有任何影响.
call by reference是将parameter的reference传给function,简单点理解就是直接把variable传给function.所以说这个variable的值是可以被function改变的.这个用法在c/c++中非常常见,用法是variable_name.
2.再来,在Java里边,你可以很简单的理解为: Java中只有call by value, 也就是说,所以所有传给function的parameter本身都不会被改变. (这是最简单直白的理解,当然也有另一种常从sun的人那边听到的说法:Java是call by value + call by reference by value)
3.那么现在的问题就是为什么第二个结果是2了. 首先说一下sun官方的解释: 对于reference type在作为parameter/argument的时候,也是call by value, 但是在你拥有足够权限时(比方说那个变量是public的, 不是final的等等各种符合的情况),可以修改这个object中fields的值(也就是属于这个object(严谨点讲是an instance of the object) 内部的变量, 在你的例子中, ko 里边的 a 就是一个field, 所以update(ko)会使ko.a变成2).
4.如果你是一个有过c/c++学习经验的人或者你以上的解释很难理解,以下这种说法或许更适合你 (当然了,这只是大多包括我在内有c经验的人的一种理解方式)
这里可以引入一个新的概念,pointer. 这是一种比较特殊的变量,它内部所储存的东西,其实只是另外一个变量的内存地址. 如果对内存没有概念,你可以把它简单理解为是风筝的线轴,虽然看它本身看不出什么端倪,但是顺着摸过去总会找到风筝,看到它是什么样子. 以pointer方式理解Java的人,通常会说: Type variable = new Type(); 这个过程中,最后生成的这个variable其实就是一个pointer,而不是instance本身.
在Java中, 有c/c++经验的人通常认为Java是call by value.同时,当一个变量用在储存reference type的时候,实际上储存的是它的pointer,这也一样可以解释为什么ko.a会有2这个结果,因为虽然pointer被传到function里边时,本身是call by value,无法被改变.但这并不影响function本身对这个pointer指向的object的内容做任何改变. 当然,再次声明,这只是一种帮助有c/c++经验的人理解的方法. Sun本身严正声明Java里边没有pointer这个东西的存在.
5. 再来解释一下为什么说楼上所说的(或者说楼上引用的)理解略有偏差.
引用"我们上面刚学习了JAVA的数据类型,则有:值类型就是按值传递的,而引用类型是按引用传递的" 这句话很明显的有两点错误. 第一点,如果我上面所说的,Java是没有call by reference的.
第二点,暂且假设Java里边是有call by reference的, 这句话依然不成立.
Java中的变量有两种类型: primitive types 和 reference type.
primitive type包括byte, short, int, long, char, boolean, float和double.
而这8种之外的所有的,都是reference type.
下面是一段对你的贴上来的code的一点延伸,希望可以帮助你更好的理解Java中的argument / parameter到底是如何运作的.
public class Test {
public static void main(String[] args) {
int a = 1;
Koo koo = new Koo();
Object o = new Integer(1);
Koo newKoo = new Koo();
update(a);
update(koo);
update(o);
update(newKoo);
newUpdate(newKoo);
System.out.println(a);
System.out.println(koo.a);
System.out.println(o);
System.out.println(newKoo.a);
}
static void update(int a) {
a++;
}
static void update(Koo koo) {
koo.a++;
}
static void update(Object o) {
o = (int) (Integer.parseInt(o.toString()) + 1);
}
static void newUpdate(Koo koo) {
koo = new Koo();
}
}
class Koo {
int a = 1;
}
/*
o = (int) (Integer.parseInt(o.toString()) + 1); 这一行中的(int)纯粹是多余的,是否有这个casting对code本身没有任何影响. 如果你高兴也可以用
o = new Integer(Integer.parseInt(o.toString()) + 1);
或者干脆
o = Integer.parseInt(o.toString()) + 1;
*/
以上这些code运行之后会得到1 2 1 2的结果. 后面两个结果可以很好的说明, 即使对objects (reference type variables) 来看, Java所应用的也并不是call by reference. 否则的话,以上code运行结果应该是1 2 2 1
希望你可以真正理解这个新的例子中,产生1212这个结果的原因,从而对Java中的arguments有一个系统全面的认识.
图片是相关资料的链接,知道里貌似不能加网址
一道java编程题
package Demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Demo9 {
/**
* 随便输入一段字符串,把出现次数最多的打印出来,如:aabbbbbbbbbcccdffff,就把b打印出来,用java代码实现
*/
public static void main(String[] args) {
// 获得输入的字符串
Scanner input = new Scanner(System.in);
String str = input.next();
// 转成字符数组处理
char[] cArray = str.toCharArray();
MapCharacter, Integer map = new HashMapCharacter, Integer();
// 统计各字符出现次数
for (char c : cArray) {
Integer count = map.get(c);
if (null == count) {//第一次在map中是不可能找到该字符串的,我们直接给Integer加1
map.put(c, 1);
} else {
map.put(c, count + 1);//之后的都先取出出现的次数,在他的Integer基础上加1
}
}
int maxCount = 0;
char mostChar = '0';
// 比较获得出现次数最多的字符
for (Map.EntryCharacter, Integer entry : map.entrySet()) {//map类型的迭代
if (entry.getValue() maxCount) {
maxCount = entry.getValue();
mostChar = entry.getKey();
}
}
// 打印出结果
System.out.println(mostChar);
}
}