楼主: ReneeBK
1184 13

Java for the Beginning Programmer [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.9304
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


Product Details

本帖隐藏的内容

Java for the Beginning Programmer.pdf (3.26 MB, 需要: 5 个论坛币)





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programmer Beginning Programme Program beginn Java

沙发
ReneeBK 发表于 2016-9-3 08:48:52 |只看作者 |坛友微信交流群
  1. /**
  2. * BadNumbers
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 4
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how to input numbers, and handle an
  10. * invalid number.
  11. *
  12. * This software is copyrighted. You may use it in programs
  13. * of your own, without restriction, but you may not
  14. * publish the source code without the author's permission.
  15. * For more information on distributing this code, please
  16. * visit:
  17. *    http://www.heatonresearch.com/hr_legal.php
  18. *
  19. * @author Jeff Heaton
  20. * @version 1.1
  21. */

  22. import java.io.*;

  23. public class BadNumbers
  24. {
  25.   public static void main(String args[])
  26.   {
  27.     try
  28.     {
  29.       InputStreamReader inputStreamReader =
  30.         new InputStreamReader ( System.in );
  31.       BufferedReader in =
  32.         new BufferedReader ( inputStreamReader );
  33.       System.out.print("Enter a length in miles? ");
  34.       String miles = in.readLine();
  35.       double dMiles = Double.parseDouble(miles);
  36.       double dKilometers = 1.609344 *dMiles;
  37.       System.out.println("That is " + dKilometers +
  38.         " kilometers.");
  39.     }
  40.     catch(NumberFormatException e)
  41.     {
  42.       System.out.println(
  43.         "You entered an invalid number.");
  44.     }
  45.     catch(IOException e)
  46.     {
  47.     }
  48.   }
  49. }
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-9-3 08:50:02 |只看作者 |坛友微信交流群
  1. /**
  2. * Hello
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 4
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how to input strings.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. public class Hello
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print("What is your name? ");
  33.       String name = in.readLine();
  34.       System.out.println("Hello " + name );
  35.     }
  36.     catch(IOException e)
  37.     {
  38.     }
  39.   }
  40. }
复制代码

使用道具

板凳
ReneeBK 发表于 2016-9-3 08:53:44 |只看作者 |坛友微信交流群
  1. /**
  2. * InvalidString
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how NOT to compare two strings.  Do not use == on strings!
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class InvalidString
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print(
  33.         "What is your favorite color? ");
  34.       String color = in.readLine();
  35.       if( color=="red" )
  36.         System.out.println(
  37.           "My favorite color is red too!");
  38.     }
  39.     catch(IOException e)
  40.     {
  41.     }
  42.   }
  43. }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-9-3 08:54:28 |只看作者 |坛友微信交流群
  1. /**
  2. * NumberCase
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how to use the switch/case.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class NumberCase
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print(
  33.         "Enter a number between 1 and 5? ");
  34.       String num = in.readLine();
  35.       int number = Integer.parseInt(num);
  36.       switch( number )
  37.       {
  38.         case 1:
  39.           System.out.println("You entered One.");
  40.           break;
  41.         case 2:
  42.           System.out.println("You entered Two.");
  43.           break;
  44.         case 3:
  45.           System.out.println("You entered Three.");
  46.           break;
  47.         case 4:
  48.           System.out.println("You entered Four.");
  49.           break;
  50.         case 5:
  51.           System.out.println("You entered Five.");
  52.           break;
  53.         default:
  54.           System.out.println(
  55.             "You did not enter a number between 1 and 5.");
  56.           break;
  57.       }
  58.     }
  59.     catch(NumberFormatException e)
  60.     {
  61.       System.out.println(
  62.         "You must enter a valid number.");
  63.     }
  64.     catch(IOException e)
  65.     {
  66.     }
  67.   }
  68. }
复制代码

使用道具

地板
ReneeBK 发表于 2016-9-3 08:55:54 |只看作者 |坛友微信交流群
  1. /**
  2. * NumberIf
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how to use "if statements" with numbers.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class NumberIf
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print(
  33.         "Enter a number between 1 and 5? ");

  34.       String num = in.readLine();
  35.       int number = Integer.parseInt(num);
  36.       if( number==1 )
  37.       {
  38.         System.out.println("You entered One.");
  39.       }
  40.       else if( number==2 )
  41.       {
  42.         System.out.println("You entered Two.");
  43.       }
  44.       else if( number==3 )
  45.       {
  46.         System.out.println("You entered Three.");
  47.       }
  48.       else if( number==4 )
  49.       {
  50.         System.out.println("You entered Four.");
  51.       }
  52.       else if( number==5 )
  53.       {
  54.         System.out.println("You entered Five.");
  55.       }
  56.       else
  57.       {
  58.         System.out.println(
  59.           "You did not enter a number between 1 and 5.");
  60.       }
  61.     }
  62.     catch(NumberFormatException e)
  63.     {
  64.       System.out.println(
  65.         "You must enter a valid number.");
  66.     }
  67.     catch(IOException e)
  68.     {
  69.     }
  70.   }
  71. }
复制代码

使用道具

7
ReneeBK 发表于 2016-9-3 08:58:20 |只看作者 |坛友微信交流群
  1. /**
  2. * OldEnough
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class checks to see if you are old enough to vote in the USA.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class OldEnough
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print("How old are you? ");
  33.       String age = in.readLine();
  34.       int iAge = Integer.parseInt(age);
  35.       if( iAge>=18 )
  36.       {
  37.         System.out.println(
  38.           "You are old enough to vote in the United States.");
  39.       }
  40.       if( iAge<18 )
  41.       {
  42.         System.out.println(
  43.           "You are not old enough to vote in" +
  44.           " the United States");
  45.       }
  46.     }
  47.     catch(NumberFormatException e)
  48.     {
  49.       System.out.println("That is not a valid age.");
  50.     }
  51.     catch(IOException e)
  52.     {
  53.     }
  54.   }
  55. }
复制代码

使用道具

8
ReneeBK 发表于 2016-9-3 08:58:55 |只看作者 |坛友微信交流群
  1. /**
  2. * StringElse
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how to use an "else" with an "if".
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class StringElse
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );
  32.       System.out.print(
  33.         "What is your favorite color? ");
  34.       String color = in.readLine();
  35.       if( color.equals("red") )
  36.       {
  37.         System.out.println(
  38.           "My favorite color is red too!");
  39.       }
  40.       else
  41.       {
  42.         System.out.println("I guess " + color +
  43.           " is okay, but I like red better.");
  44.       }
  45.     }
  46.     catch(IOException e)
  47.     {
  48.     }
  49.   }
  50. }
复制代码

使用道具

9
ReneeBK 发表于 2016-9-3 09:00:57 |只看作者 |坛友微信交流群
  1. /**
  2. * ValidString
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 5
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows the correct way to compare a string.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import java.io.*;

  22. class ValidString
  23. {
  24.   public static void main(String args[])
  25.   {
  26.     try
  27.     {
  28.       InputStreamReader inputStreamReader =
  29.         new InputStreamReader ( System.in );
  30.       BufferedReader in =
  31.         new BufferedReader ( inputStreamReader );

  32.       System.out.print(
  33.         "What is your favorite color? ");

  34.       String color = in.readLine();

  35.       if( color.equals("red") )
  36.         System.out.println(
  37.           "My favorite color is red too!");
  38.     }
  39.     catch(IOException e)
  40.     {
  41.     }
  42.   }
  43. }
复制代码

使用道具

10
ReneeBK 发表于 2016-9-3 09:06:56 |只看作者 |坛友微信交流群
  1. /**
  2. * MethodCall
  3. * Copyright 2006 by Jeff Heaton(jeff@jeffheaton.com)
  4. *
  5. * Example program from Chapter 6
  6. * Java for the Beginning Programmer
  7. * http://www.heatonresearch.com/articles/series/15/
  8. *
  9. * This class shows how objects can be used to return values from methods.
  10. *
  11. * This software is copyrighted. You may use it in programs
  12. * of your own, without restriction, but you may not
  13. * publish the source code without the author's permission.
  14. * For more information on distributing this code, please
  15. * visit:
  16. *    http://www.heatonresearch.com/hr_legal.php
  17. *
  18. * @author Jeff Heaton
  19. * @version 1.1
  20. */

  21. import javax.swing.*;

  22. public class MethodCall
  23. {
  24.   static void changeValue(JButton button)
  25.   {
  26.     // Change the text of the button, this
  27.     // new value is reflected outside of the
  28.     // call to "changeValue"
  29.     button.setText("New value");
  30.   }

  31.   static void changeReference(JButton button)
  32.   {
  33.     // Create a new button, and assign its
  34.     // reference to "button". This change is
  35.     // not reflected outside of the call
  36.     // to "changeValue"
  37.     button = new JButton("New value");
  38.   }

  39.   static void changePrimitive(int i)
  40.   {
  41.     i = i + 1;
  42.   }

  43.   /**
  44.    * Main entry point for example.
  45.    * @param args Not used.
  46.    */
  47.   public static void main(String args[])
  48.   {
  49.     // setup the variables
  50.     JButton button1 = new JButton("Old Value");
  51.     JButton button2 = new JButton("Old Value");
  52.     int var = 5;
  53.     // call the methods
  54.     changeValue(button1);
  55.     changeReference(button2);
  56.     changePrimitive(var);
  57.     // display the new values
  58.     System.out.println("Button1:" +
  59.     button1.getText());
  60.     System.out.println("Button2:" +
  61.     button2.getText());
  62.     System.out.println("Primitive variable:" + var);
  63.   }
  64. }
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-4 09:12