Java面向对象程序设计-实验1Java入门

第1关:输出HelloWorld!

说明: 1.Java是纯粹的面向对象的编程语言,所有的操作都在某个类中实现。 2.Java源程序中的语句所涉及的括号等标点符号都要在英文状态下输入;Java源程序代码区分大小写! 3.如果源文件中有多个类,那么最多有一个类是public类,且该public类名必须与其所有的文件名相同。 4.public类中有一个main方法(public static void main(String args[])),是程序运行的起点。 5.基本输出语句:带换行的输出,如System.out.println("大家好");

本关任务: 创建一个public类,类名为:Hello 在该类中创建main方法 在main方法中,编写一条输出语句,使得换行输出为:HelloWorld!

代码

/*创建一个public类,类名为:Hello
在该类中创建main方法
在main方法中,编写一条输出语句,使得换行输出为:HelloWorld!
*/
public class Hello{
    public static void main(String[] args) {

System.out.print("HelloWorld!");
        
    } 
}

第2关:StudentDemo

说明: 1.当一个Java文件有多个类时,只能有一个类是public的,其他类的类名前不可加public。 2.创建一个类(无构造方法)的对象以及调用类的方法,

***本关任务: 仿照以上示例代码创建两个类并且实现在main方法中调用另外一个类的方法。 1.创建一个非public类,类名为:Student,其中有一个方法为: public void speak(String s){ System.out.println(s); } 2.再创建一个public类,类名为:StudentDemo,在其main方法中,实现如下输出: 大家好 Nice to meet you. 3.在main方法中,创建Student类的一个对象,并调用其方法speak,输出: We are students.

代码

/*创建一个非public类,类名为:Student,其中有一个方法为:
public void speak(String s){
    System.out.println(s);
}
*/

/*再创建一个public类,类名为:StudentDemo,在其main方法中,实现如下输出:
大家好
Nice to meet you.
*/

/*在main方法中,创建Student类的一个对象,并调用其方法speak,输出:
We are students.
*/
class Student{
    public void speak(String s){
        System.out.println(s);
    }
}
public class StudentDemo{
    public static void main(String[] args) {
    System.out.println("大家好!");
    System.out.println("Nice to meet you.");
    Student student = new Student();
    student.speak("We are students.");    
    }
}

第3关:定义不同类型的变量,并输出

//按照任务要求,定义不同的变量,分别输出,查看结果,总结不同变量的应用。
//此线上任务完成后,再将此程序在idea环境中运行,并改变数值与类型,体会不同数据类型的范围与区别。
 public class SY1_3 { 
    public static void main(String args[]) { 
      byte b=0x55; 
      short s=0x55ff; 
      int i=1000000; 
      long l=0xffffL; 
      char c='a'; 
      float f=0.23F; 
      double d=0.7E-3; 
      boolean B=true; 
      String S="这是字符串类数据类型"; 
      System.out.println("字节型变量 b = "+b); 
      System.out.println("短整型变量 s = "+s); 
      System.out.println(" 整型变量 i = "+i); 
      System.out.println("长整型变量 l = "+l); 
      System.out.println("字符型变量 c = "+c); 
      System.out.println("浮点型变量 f = "+f); 
      System.out.println("双精度变量 d = "+d); 
      System.out.println("布尔型变量 B = "+B); 
      System.out.println("字符串类对象 S = "+S); 
    } 
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小柒_02

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值