java新手笔记5 类
1.进制转换
/*
企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,
低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;
40万到60万之间时高于40万元的部分,可提成3%;
60万到100万之间时,高于60万元的部分,可提成1.5%,
高于100万元时,超过100万元的部分按1%提成,
从键盘输入当月利润I,求应发放奖金总数?
*/
import java.util.Scanner;
public class Demo1 { public static void main(String args[]) {
/*
System.out.println("二进制:4 " +Integer.toBinaryString(4));
System.out.println("八进制:16 " +Integer.toOctalString(16));
System.out.println("十六进制:10 " + Integer.toHexString(10));
int a = 12328;
int b,c,d,e,f;
//取个位
b = a % 10;
//c = a % 100 / 10;
c = a / 10 % 10;
d = a / 100 % 10;
e = a / 1000 % 10;
f = a / 10000;
System.out.println("b = " + b + "\tc = " + c);
System.out.println("d = " + d + "\te = " + e + "\tf = " + f); int a = 462,b,c,d,e,f;
if(a < 10){
System.out.println("1位数...");
} else if ( a < 100 ) {
b = a % 10;
c = a / 10 % 10;
System.out.println("2位数..." + b + c);
} else if ( a < 1000 ) {
b = a % 10;
c = a / 10 % 10;
d = a / 100 % 10;
System.out.println("3位数..." + b + c + d);
} else if ( a < 10000 ) {
b = a % 10;
c = a / 10 % 10;
d = a / 100 % 10;
e = a / 1000 % 10;
System.out.println("4位数..." + b + c + d + e);
} else {
b = a % 10;
c = a / 10 % 10;
d = a / 100 % 10;
e = a / 1000 % 10;
f = a / 10000;
System.out.println("5位数..." + b + c + d + e + f);
} Scanner scan = new Scanner(System.in);
System.out.println("请输入你信息姓名、年龄、成绩:");
String name = scan.next();//获取字符串
int age = scan.nextInt();//获取整型数字
double score = scan.nextDouble();
System.out.println("输入的信息如下:");
System.out.println("姓名:" + name + " 年龄:" + age + " 成绩:" + score); Scanner scan = new Scanner(System.in);
System.out.println("请输入利润:");
double I = scan.nextDouble();
double bound = 0;
//奖金方案
if(I <= 10) {
bound = I * 0.1;
} else if( I < 20 ) {
bound = 10 * 0.1 + (I - 10) * 0.075; // 35 10 10 0.075
} else if( I < 40 ) {
bound = 10 * 0.1 + 10 * 0.075 + (I - 20) * 0.05;
} else if( I < 60 ) {
bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (I - 40) * 0.03;
} else if( I < 100 ) {
bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (I - 60) * 0.015;
} else {
bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (I - 100) * 0.01;
}
System.out.println("奖金:" + bound);
//String s = NULL;
*/
int a = 1, b = 6, c = 9;
if(a > b){
if(a > c){
if(b > c){
System.out.println("@:" + a + b + c);
} else {
System.out.println(" :" + a + c + b);
}
} else {
System.out.println(" :" + c + a + b);
}
} else {
if(b > c) {
if( a > c){
System.out.println(" :" + b + a + c);
} else {
System.out.println(" :" + b + c + a);
} } else {
System.out.println("** :" + c + b + a);
} } int year = 2012;
boolean isLeapYear = year % 400 == 0 || year % 4 == 0 && year % 100 != 0; }
}
2.封装类
//声明类 类封装
public class Student { String name;
char sex;
private int age;//属性私有
int sno;
//提供访问属性的方法 通过方法防止非法数据存入属性
void setAge() {
age = 30;
if(age > 150 || age < 0){
System.out.println("年龄不合法...");
age = 0;
} } int getAge() {
return age; //返回age属性值
} void study() {
System.out.println("学习!...");
} //方法
void speak() {
System.out.println("大家好!...");
} }
3.测试类
//声明类
public class StudentTest {
public static void main(String[] args){
Student o = new Student();//创建自定义对象
//o 学生对象 Student 类 int a = 20; System.out.println("o.name = " + o.name);
o.speak();//方法调用
o.study(); o.name = "张三";
o.sex = '男';
//o.age = -20;
o.setAge();
o.sno = 180; o.name = "张飞";
System.out.println("o.name = " + o.name);
System.out.println("o.sex = " + o.sex);
//System.out.println("o.age = " + o.age);
System.out.println("o.getAge() = " + o.getAge());
System.out.println("o.sno = " + o.sno); }
}
4.测试类1
//声明类
public class StudentTest1 {
public static void main(String[] args){
Student s = null;//声明对象的引用
//s.study(); new
s = new Student();//new 创建对象 实例化对象
s.name = "李四";
System.out.println("s.name = " + s.name);
s.study(); Student s2 = new Student();
s2.name = "张三";
System.out.println("s2.name = " + s2.name);
System.out.println("s.name = " + s.name);
s2.study(); }
}
5.类说明
//声明类
public class Obj { //状态 属性 实例变量
String name;
char sex;
int age; //行为 方法 动作
void speak() {
System.out.println("大家好!...");
} }
//////////////////////////////////
class ObjTest {
public static void main(String[] args){
Obj o = new Obj();//创建自定义对象
//Random ran = new Random(); System.out.println("o.name = " + o.name);
o.speak();
o.name = "张三";
o.sex = '男';
o.age = 20; o.name = "张飞";
System.out.println("o.name = " + o.name);
System.out.println("o.sex = " + o.sex);
System.out.println("o.age = " + o.age); }
}
6.类方法
//声明类
public class MethodTest1 { int a = 10;//与对象关联
//方法 无参数 无返回值
void method1() {
System.out.println("call method1()");
}
//返回值类型 add 方法名 ()传入参数
void add (int a, int b) {
int sum = a + b;
System.out.println("sum = " + sum);
}
//形参列表 数据类型 变量名
double caluSal(int job,int house,int bound) {
//System.out.println("sum = " + job + house + bound);
return job + house + bound;// return返回计算结果
} public static void main(String[] args){
MethodTest1 mt = new MethodTest1();
int b = mt.a;
System.out.println("b = " + b);
mt.method1();
mt.add(10, 6);//调用时 参数类型 个数与声明的方法匹配 double salary = mt.caluSal(8000,5000,6000); System.out.println("salary = " + salary);
//结果返回还需要处理
double result = salary * 0.05; System.out.println("result = " + result);
}
}
java新手笔记5 类的更多相关文章
- java新手笔记18 类比较
1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 // ...
- java新手笔记9 类的封装示例
1.bank类 package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 ...
- java新手笔记11 类的静态属性、方法(单例)
1.Person类 package com.yfs.javase; public class Person { String name;//每个对象上分配 与对象绑定 int age; char se ...
- java新手笔记14 类继承示例
1.Person package com.yfs.javase; public class Person { private String name; private int age; private ...
- Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
- Java学习笔记之---类和对象
Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态 例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...
- java新手笔记8 包
1.main函数 public class MainParam { //考察main 方法的参数 args //运行时可以传入参数 参数类型 String public static void mai ...
- java新手笔记3 运算符&循环
1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.pri ...
- java新手笔记31 集合实现类
Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...
随机推荐
- 解决 MyEclipse 10 中 JSp页面 “return false” 报错问题
1.MyEclipse ->. Preferences 2.validation ->>找到JavaScript validator for Js files builder 下面 ...
- Java笔记(二十一)……String与StringBuffer
String类 String类是一个特殊的类,叫做只读类,一旦创建了对象,便不可被改变,同样"abc"既为一个常量,也为一个对象,也是不可以改变的 String s1 = &quo ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- Storm系列(三)Topology提交过程
提交示例代码: 1 ); // 设置一个ack线程 9 conf.setDebug(true); // 设置打印所有发送的消息及系统消息 10 StormSubmitter.su ...
- Spring-demo1(初学者的尝试,2015.03.19)
项目结构: 源代码如下: package com.bean; public interface Person { public void Speak(); } package com.bean; pu ...
- HW2.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { final dou ...
- Yii防注入攻击笔记
网站表单有注入漏洞须对所有用户输入的内容进行个过滤和检查,可以使用正则表达式或者直接输入字符判断,大部分是只允许输入字母和数字的,其它字符度不允许:对于内容复杂表单的内容,应该对html和script ...
- C#- 泛型去除重复项
今天被这个问题纠结了好一会.如何去除重复项,我遇到的问题是,在判断是否重复的条件是有两个,一个信息来源,一个是信息标题. 最后使用了哈希后很好的解决,感觉挺高效的.代码贴下,做一个备忘 //防止群发, ...
- 非刚性图像配准 matlab简单示例 demons算法
2011-05-25 17:21 非刚性图像配准 matlab简单示例 demons算法, % Clean clc; clear all; close all; % Compile the mex f ...
- 史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~
继续延续坑爹标题系列.其实只是把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...