用java编写一个简单例子,题目如下
package test;
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
/**
* sayHello方法
*/
public void sayHello() {
System.out.println("学号为" + this.id + "的同学的具体信息如下:");
System.out.println("姓名:" + this.name);
System.out.println("班级:" + this.clazz);
System.out.println("年龄:" + this.age);
System.out.println("家庭住址:" + this.address);
}
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 第一问
Student student = new Student();
student.setAddress("百度知道");
student.setAge(1);
student.setClazz("一班");
student.setId("071251000");
student.setName("lsy605604013");
student.sayHello();
// 第二问
Student studentNew = new Student();
studentNew.setAddress("搜搜知道");
studentNew.setAge(2);
studentNew.setClazz("二班");
studentNew.setId("071251001");
studentNew.setName("lady");
if (student.getAge() studentNew.getAge())
studentNew.sayHello();
else if (student.getAge() studentNew.getAge())
student.sayHello();
else
System.out.println("两人一样大");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
5道简单的JAVA编程题(高分悬赏)
很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。
(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One
2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i=100;i+=2)
System.out.println(i);
}
}
(2)while语句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i = 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while语句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i=100);
}
}
3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while语句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while语句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}while(i10);
System.out.println("最大值:" + max);
}
}
4、编写程序,计算从1到100之间的奇数之和。
(1)for循环
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(2)while语句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i = 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(3)do…while语句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i = 100);
System.out.println("1~100间奇数和:" + sum);
}
}
5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。
(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}
用java或者c程序输出如下图的这种简单心形,谁能给我讲讲思路。加上程序示例更好
用方程的话肯定输出不会和你的图完全一致
心形线方程如图
用上面行第二个比较简单,程序如下
#include stdio.h
bool draw(float x, float y)
{
float a = x * x + y * y - 1.0;
float b = x * x * y * y * y;
return a * a * a - b = 0;
}
int main(int argc, char* argv[])
{
for (float y = 1.5; y = -1.5; y -= 0.1)
{
for (float x = -1.2; x = 1.2; x += 0.05)
{
if (draw(x, y))
{
bool left = draw(x - 0.05, y);
bool right = draw(x + 0.05, y);
bool up = draw(x, y + 0.1);
bool down = draw(x, y - 0.1);
if (left right up down)
printf(" ");
else
printf("*");
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
draw函数是判断(x,y)坐标是否在心形范围内
主函数循环,y取值±1.5之间,步长0.1,x取值±1.2之间,步长0.05
如果(x,y)坐标在心形范围外打印空格
如果(x,y)坐标在心形范围内,由于这个心是空心的,要继续判断是不是心形边缘,判断周围4个点坐标,如果都在范围内,表示(x,y)坐标不会是边缘,打印空格,否则是边缘,打印星号
最终结果如图
JAVA编程
akfucc 很速度啊,不过我也写了,楼主自己比较一下吧。
------------------------- Demo.java ----------------------------
package rectsearch;
import java.text.DecimalFormat;
/**
* @author: shuangwhywhy
* @purpose: 定义一个矩形
* @param: left, bottom, right, top
* @instr: 矩形由四块同样大小的小矩形组成,分别为左下块、右下块、右上块、左上块。
* 所有的矩形都有左、下、右、上四个坐标值表述。
*/
class Rectangle {
public double left, bottom, right, top;
private static DecimalFormat df = new DecimalFormat("0.0000"); //用以格式化输出
public Rectangle (double left, double bottom, double right, double top) {
this.left = left;
this.bottom = bottom;
this.right = right;
this.top = top;
}
/**
* 获得当前矩形的左下块
*/
public Rectangle getLeftBottom () {
return new Rectangle(left, bottom, (left+right)/2, (bottom+top)/2);
}
/**
* 获得当前矩形的右下块
*/
public Rectangle getRightBottom () {
return new Rectangle((left+right)/2, bottom, right, (bottom+top)/2);
}
/**
* 获得当前矩形的右上块
*/
public Rectangle getRightTop () {
return new Rectangle((left+right)/2, (bottom+top)/2, right, top);
}
/**
* 获得当前矩形的左上块
*/
public Rectangle getLeftTop () {
return new Rectangle(left, (bottom+top)/2, (left+right)/2, top);
}
/**
* 判断所给矩形是否被当前矩形完全覆盖
*/
public boolean contains (Rectangle rect) {
if (left rect.left || right rect.right || bottom rect.bottom || top rect.top)
return false;
return true;
}
/**
* 获得当前矩形的字符串表述
* 此处用到了 DecimalFormat 的静态对象以格式化数值
*/
public String toString () {
return ("(" + df.format(left) + ", " + df.format(bottom) + ", " + df.format(right) + ", " + df.format(top) + ")");
}
}
/**
* 此类执行搜索
*/
public class Search {
private Rectangle rectMap, rectTarget;
public Search (Rectangle rectMap, Rectangle rectTarget) {
this.rectMap = rectMap;
this.rectTarget = rectTarget;
validate(); //验证有效性
}
/**
* 验证有效性。输入的矩形须满足以下条件:
* - 不为 null
* - map 完全覆盖 target
* 否则程序报错并退出。
*/
public void validate () {
if (rectMap == null || rectTarget == null) {
System.out.println("Invalid rectangle rectMap = " + rectMap + ", rectTarget = " + rectTarget);
System.exit(0);
} else if (!rectMap.contains(rectTarget)) {
System.out.println("The target rectangle " + rectTarget.toString() + " does not entirely lie inside the map " + rectMap.toString() + ".");
System.exit(0);
}
}
/**
* 公共方法调用私有成员方法 find(Rectangle)
* 外部用此方法是因为这样更符合逻辑
*/
public void find () {
find(rectMap);
}
/**
* 递归缩小搜索区域
* 找到(搜索区域不能完全覆盖目标矩形)时退出程序
* 否则分别对当前 rect 的左下块、右下块、右上块、左上块递归调用此方法。
*/
private void find (Rectangle rect) {
if (!rect.contains(rectTarget)) {
return;
}
System.out.println("---" + rect.toString());
find(rect.getLeftBottom());
find(rect.getRightBottom());
find(rect.getRightTop());
find(rect.getLeftTop());
}
}
-------------------- SearchDemo.java ---------------------------
package rectsearch;
import java.io.*;
import java.util.StringTokenizer;
/**
* 实现类
*/
public class SearchDemo {
public static void main (String args[]) throws IOException, NumberFormatException {
// 获取用户输入 map 的四个坐标:
System.out.print("Enter coordinates of the map (left, bottom, right, top): ");
String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
StringTokenizer strtoken = new StringTokenizer(input, " ");
if (strtoken.countTokens() 4) { //判断输入的是否是 4 个数
System.out.println("Please input 4 double values! Run again!");
System.exit(0);
}
double left = Double.parseDouble(strtoken.nextToken());
double bottom = Double.parseDouble(strtoken.nextToken());
double right = Double.parseDouble(strtoken.nextToken());
double top = Double.parseDouble(strtoken.nextToken());
Rectangle rectMap = new Rectangle(left, bottom, right, top);
// 获取用户输入 target 的四个坐标:
System.out.print("Enter coordinates of the target (left, bottom, right, top): ");
input = new BufferedReader(new InputStreamReader(System.in)).readLine();
strtoken = new StringTokenizer(input, " ");
if (strtoken.countTokens() 4) { //判断输入的是否是 4 个数
System.out.println("Please input 4 double values! Run again!");
System.exit(0);
}
left = Double.parseDouble(strtoken.nextToken());
bottom = Double.parseDouble(strtoken.nextToken());
right = Double.parseDouble(strtoken.nextToken());
top = Double.parseDouble(strtoken.nextToken());
Rectangle rectTarget = new Rectangle(left, bottom, right, top);
System.out.println();
// 实例化搜索类并执行搜索:
Search search = new Search(rectMap, rectTarget);
search.find();
}
}
--------------------------------------------------------------
运行示例: