计算各种图形的周长(接口与多态)(SDUT 3338)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.nextLine();
String[] strs = str.split(" ");
double []a = new double [100];
int i;
for(i = 0; i < strs.length; i++)
a[i] = Integer.parseInt(strs[i]);
double x,y,z;
if(i == 1){
x = a[0];
if(x <= 0)
System.out.println("0.00");
else{
Circle c = new Circle(x);
System.out.printf("%.2f\n",c.length());
}
}
else if(i == 2){
x = a[0];
y = a[1];
if(x <= 0)
System.out.println("0.00");
else{
Rectangle r = new Rectangle(x,y);
System.out.printf("%.2f\n",r.length());
}
}
else if(i == 3){
for(i = 0; i < 3; i++){
for(int j = 0; j < i; j++){
if(a[j] > a[j+1]){
double t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
x = a[0];
y = a[1];
z = a[2];
if(x + y > z ){
if(x <= 0)
System.out.println("0.00");
else{
Triangle T = new Triangle(x,y,z) ;
System.out.printf("%.2f\n",T.length());
}
}
else
System.out.println("0.00");
}
}
in.close();
}
}
interface Shape{
//public void shape();
double length(); }
class Triangle implements Shape{
double x1,y1,z1;
Triangle(double x1, double y1 , double z1){
this.x1 = x1;
this.y1 = y1;
this.z1 = z1;
} public double length(){
return x1 + y1 + z1;
}
}
class Rectangle implements Shape{
double x,y;
Rectangle(double x, double y){
this.x = x;
this.y = y;
} public double length(){
return (x + y) * 2;
}
}
class Circle implements Shape{
double z;
Circle(double z){
this.z = z;
}
public double length(){
return 2 * 3.14 * z;
}
}

计算长方形的周长和面积(类和对象)(SDUT 3339)

import java.util.*;

class Rect{
public int n;
public int m;
Rect(int n){
System.out.println(n+" "+n+" "+4*n+" "+n*n);
}
Rect(int n, int m){
System.out.println(n+" "+m+" "+(2*n+2*m)+" "+m*n);
}
} public class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
while ( scanner.hasNext() ){
String h = scanner.nextLine();
char[] a = h.toCharArray();
int temp = 0;
int i;
for ( i = 0;i < a.length; i++ ){
if ( a[i] == ' ' )
temp = 1;
}
if ( temp == 0 ){
int b = Integer.parseInt(h);
if ( b <= 0 )
System.out.println("0 0 0 0");
else{
Rect h1 = new Rect(b);}
}
else {
String[] h3 = h.split(" ");
int e = Integer.parseInt(h3[0]);
int r = Integer.parseInt(h3[1]);
if ( e <= 0||r <= 0 )
System.out.println("0 0 0 0");
else{
Rect h1 = new Rect(e,r);}
}
}
}
}

X 答答租车系统(面向对象综合练习)(SDUT 3349)

import java.util.*;

class Car
{
int name, money, passenger;
double weight; public Car(int name, int money, double weight, int passenger)
{
this.name = name;
this.money = money;
this.passenger = passenger;
this.weight = weight;
} } public class Main
{ public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Car [] c =
{
new Car(0, 5, 0, 800),
new Car(1, 5, 0, 400),
new Car(2, 5, 0, 800),
new Car(3, 51, 0, 1300),
new Car(4, 55, 0, 1500),
new Car(5, 5, 0.45, 500),
new Car(6, 5, 2, 450),
new Car(7, 0, 3, 200),
new Car(8, 0, 25, 1500),
new Car(9, 0, 35, 2000)
};
int peoplesum = 0;
int moneysum = 0;
double weightsum = 0;
int order = sc.nextInt();
if(order == 1)
{
int num = sc.nextInt();
for(int i = 0; i < num; i++)
{
int j = sc.nextInt() - 1;
int day = sc.nextInt();
moneysum += c[j].money * day;
peoplesum += c[j].passenger * day;
weightsum += c[j].weight * day;
}
System.out.println(moneysum + " " + String.format("%.2f", weightsum) + " " + peoplesum);
}
else
{
System.out.println(moneysum + " " + String.format("%.2f", weightsum) + " " + peoplesum);
}
sc.close();
}
}

Y   分数四则运算 (SDUT 3849)

import java.util.Scanner;
import java.text.DecimalFormat; class Sum {
int x1, y1, x2, y2;
char str; Sum(int n1, int m1, int n2, int m2, char op) {
x1 = n1;
x2 = n2;
y1 = m1;
y2 = m2;
str = op;
} int getGcd(int a, int b) {
int n = a, m = b;
while (m > 0) {
int x = n;
n = m;
m = x % m;
}
return n;
} void getAns() {
int x = getGcd(y1, y2);
int a, b, c, d, ans1 = 0, ans2 = 0;
a = x1;
b = y1;
c = x2;
d = y2;
if (str == '+' || str == '-') {
int lcm = b * d / x;
a = a * d / x;
c = c * b / x;
if (str == '+') {
ans1 = a + c;
} else if (str == '-') {
ans1 = a - c;
}
ans2 = lcm;
if (ans1 < 0)
x = -ans1;
else
x = ans1;
x = getGcd(x, ans2);
if (ans1 % x == 0 && ans2 % x == 0) {
ans1 /= x;
ans2 /= x;
}
} else if (str == '*') {
a = a * c;
b = b * d;
ans1 = a;
ans2 = b;
int temp = getGcd(ans1, ans2);
if (ans1 % temp == 0 && ans2 % temp == 0) {
ans1 /= temp;
ans2 /= temp;
}
} else if (str == '\\') {
int temp = c;
c = d;
d = temp;
a = a * c;
b = b * d;
ans1 = a;
ans2 = b;
int te = getGcd(ans1, ans2);
if (ans1 % te == 0 && ans2 % te == 0) {
ans1 /= te;
ans2 /= te;
}
} if (ans1 == 0 && ans1 != ans2 || ans2 == 1)
System.out.println(ans1);
else if (ans1 == ans2)
System.out.println(1);
else
System.out.println(ans1 + "/" + ans2);
}
} public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// DecimalFormat df = new DecimalFormat(".00");
Sum p;
String s;
char op = 0;
while (sc.hasNext()) {
s = sc.next();
int temp = 0;
int xx[] = new int[5];
int top = 0;
int x1, x2, y1, y2;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
xx[top] = xx[top] * 10 + (s.charAt(i) - '0');
} else {
top++;
if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '\\')
op = s.charAt(i);
}
}
x1 = xx[0];
y1 = xx[1];
x2 = xx[2];
y2 = xx[3];
// System.out.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + op);
p = new Sum(x1, y1, x2, y2, op);
p.getAns();
}
}
}

Z    Shift Dot   (SDUT 3848)

import java.util.*;

class Dot {
int x, y; Dot() { } Dot(int n, int m) {
x = n;
y = m;
} void getAns(int a, int b) {
x += a;
y += b;
} void Print() {
System.out.println("(" + x + "," + y + ")");
}
} public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int x, y, n, a, b;
while (sc.hasNext()) {
x = sc.nextInt();
y = sc.nextInt();
n = sc.nextInt();
Dot p = new Dot(x, y);
for (int i = 0; i < n; i++) {
a = sc.nextInt();
b = sc.nextInt();
p.getAns(a, b);
}
p.Print();
}
}
}

Java面向对象5(V~Z)的更多相关文章

  1. Java.lang.NoSuchMethodError: 后带 V/Z等字母的

    知道 Java.lang.NoSuchMethodError: 后带 V/Z等字母的 错误,一般都是 jar包冲突引起的,找到冲突的jar包,去掉一个就好

  2. java 面向对象 2

    一.JAVA类的定义 JAVA里面有class关键字定义一个类,后面加上自定义的类名即可.如这里定义的person类,使用class person定义了一个person类,然后在person这个类的类 ...

  3. Java面向对象 IO (四)

     Java面向对象  IO  (四) 知识概要:                 (1)打印流 (2)序列流 SequenceInputStream (3)ObjectInputStream与Ob ...

  4. Java面向对象 其他对象

     Java面向对象  其他对象 知识概要:             (1)可变参数 (2)静态导入 (3)System (4)Runtime (5)Date  Calendar (6)Math 本 ...

  5. Java面向对象 集合(下)

      Java面向对象 集合(下) 知识概要:               (1)Map集合的体系结构 (2)Map集合的方法 (3)HashMap TreeMap (4)集合框架中的常用工具类 ( ...

  6. Java面向对象 集合(上)

     Java面向对象  集合(上) 知识概要:             (1)体系概述 (2)共性方法 (3)迭代器 (4)list集合 (5)Set 集合 体系概述:              集 ...

  7. Java面向对象 继承(上)

       Java面向对象 继承 知识概要:         (1)继承的概述 (2)继承的特点 (3)super关键字 (4)函数覆盖 (5) 子类的实例化过程 (6) final关键字 (1)继承 ...

  8. Java面向对象----个人参考资料

    Java面向对象 :什么是面向对象.类与对象.封装.构造方法.static关键字.继承.抽象类.接口.多态 一.什么是面向对象 1.面向过程思想 面向过程:(PO,Procedure Oriented ...

  9. 2017-2018-2 20165318 实验三《Java面向对象程序设计》实验报告

    2017-2018-2 20165318 实验三<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计        班级:1653班        姓名:孙晓暄  ...

  10. 20145122《Java面向对象程序设计》实验二实验报告

    实验名称: Java面向对象程序设计 实验内容: 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 PSP时间 步骤 ...

随机推荐

  1. Scrapy里Selectors 四种基础的方法

    在Scrapy里面,Selectors 有四种基础的方法xpath():返回一系列的selectors,每一个select表示一个xpath参数表达式选择的节点css():返回一系列的selector ...

  2. iframe/frameset/frame的区别

    目录 iframe iframe属性的用法 iframe属性的取值 iframe的书写格式 frameset frameset的用法(框架模板) frameset属性的属性值 frame frame的 ...

  3. cygwin gcc 编译windowsAPI 报错的一个解决方案

    一开始按照linux的习惯去编译一个使用了windowsAPI的程序 结果提示: $ i686-pc-cygwin-g++ screen_catch.cscreen_catch.c: In funct ...

  4. Python 常用外部模块详解

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  5. luogu题解 P4092 【[HEOI2016/TJOI2016]树】树链剖分

    题目链接: https://www.luogu.org/problemnew/show/P4092 瞎扯--\(O(Q \log^3 N)\)解法 这道先yy出了一个\(O(Q \log^3 N)\) ...

  6. TypeScript入门二:基本数据类型

    浅析基本数据类型 TypeScript类型解析 一.浅析基本数据类型 首先有一个问题TypeScript是一门编译型语言?还是解释性语言?显然已经不能被这两个分类来区分,TypeScript的并不是为 ...

  7. Linux学习(一)-安装vm虚拟机以及如何在虚拟机上安装Centos系统

    (一)基本说明 学习Linux需要一个环境,我们需要创建一个虚拟机,然后在虚拟机上安装一个Centos系统来学习. 1)安装软件vm12; 2)通过vm12创建一个虚拟机空间; 3)在vm12创建好的 ...

  8. android项目笔记整理(2)

    31.利用SharedPreferences存储时间     读取时间:     SharedPreferences sp=this.getSharedPreferences("actm&q ...

  9. 操作xml文件

    http://www.cnblogs.com/ 一.xml文件体系如下: <?xml version="1.0" encoding="utf-8" ?&g ...

  10. Istio调用链埋点原理剖析—是否真的“零修改”分享实录(下)

    调用链原理和场景 正如Service Mesh的诞生是为了解决大规模分布式服务访问的治理问题,调用链的出现也是为了对应于大规模的复杂的分布式系统运行中碰到的故障定位定界问题.大量的服务调用.跨进程.跨 ...