Silent's Program Thread

View previous topic View next topic Go down

Silent's Program Thread

Post  SilentNite17 on Tue Feb 21, 2012 6:46 pm

Hey, everyone!

This is where I will post all of my codes that I make in Java. I may also post questions. Feel free to critique.

Thanks!

-Silent
avatar
SilentNite17
Admin

Posts : 21
Join date : 2012-02-20
Location : Behind you.

View user profile http://gennetdwell.forumotion.com

Back to top Go down

Re: Silent's Program Thread

Post  SilentNite17 on Fri Mar 02, 2012 1:01 am

Hey, guys!

Just finished a new code. This one goes with a math lesson I had recently, on arithmetic sequences. It asks you for you sequence and automaitically finds the difference, and then asks you for the nth term that you want to find in the sequence.

Remember, the formula for finding the nth term in a sequence is

a(sub n)=a(sub 1) + (n-1) d

or

a[n] = a[1] + (n-1) d

where n is the term you want to find and d is the common difference in the sequence.

Here is the code:

Code:
import java.util.Scanner;

public class ArithmeticSequenceTerms {
   
   public static void main(String[] arg)  {
      
      int[] a = new int[99];  //array used to store values
      int calc;  //just a calculation to make the computer understand it better
      int num1, num2, num3, num4, num5;
      int value, place;  //finished value, place in the array
      int d;  //the common difference in the sequence
      
      Scanner scan = new Scanner(System.in);
      
      System.out.println("This program will find the nth term in an arithmetic sequence of numbers. An arithemtic sequence is a sequence of numbers, such as 2, 4, 6, 8, etc.");
       System.out.println("The first thing you need to do is enter the first five numbers in you sequence. Please enter them now: ");
      
       System.out.println("First number?");
       num1 = scan.nextInt();
      
       System.out.println("Second?");
       num2 = scan.nextInt();
      
       System.out.println("Third?");
       num3 = scan.nextInt();
      
       System.out.println("Fourth?");
       num4 = scan.nextInt();
      
       System.out.println("Fifth, or last?");
       num5 = scan.nextInt();
      
       d = (num2 - num1);
      
       a[0] = num1;
       a[1] = num2;
       a[2] = num3;
       a[3] = num4;
       a[4] = num5;
      
       System.out.println("The next thing you need to do is tell me what term number in the sequence you want to find. For example, in the sequence 2, 4, 6, 8... then the 6th term is equal to 12.");
       System.out.println("What place do you want to find?");
       place = scan.nextInt();
      
       System.out.println("Thanks, the computer will now do the rest of the calculations for you.");
       System.out.println();
       System.out.println();
      
       calc = (place - 1) * d;
       value = (a[0]) + calc;
      
       place = place - 1;
      
       a[place] = value;
      
       System.out.print("The value of the " + place + "th term is " + value + ".");
       
   }
   

}
I really like this code.

What do you think?

-Silent
avatar
SilentNite17
Admin

Posts : 21
Join date : 2012-02-20
Location : Behind you.

View user profile http://gennetdwell.forumotion.com

Back to top Go down

Re: Silent's Program Thread

Post  Rabees on Sat Mar 03, 2012 8:57 pm

I've never done Java or heard of arithmetic sequences but it looks good. The syntax seems very similar to C#. :)

Rabees
New
New

Posts : 3
Join date : 2012-02-21
Age : 21
Location : Ohio

View user profile

Back to top Go down

Re: Silent's Program Thread

Post  SilentNite17 on Sat Mar 03, 2012 9:23 pm

I've never looked at C#, but I have looked at C++. Post a code snippet for me, if you would.
avatar
SilentNite17
Admin

Posts : 21
Join date : 2012-02-20
Location : Behind you.

View user profile http://gennetdwell.forumotion.com

Back to top Go down

Re: Silent's Program Thread

Post  Rabees on Sat Mar 03, 2012 11:26 pm

I've got a bit of experience in C++ too but that stuff is hard so I gave up and got into C#. lol.

Here's what a really dumb and useless console program looks like in C#:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;

            Console.WriteLine("svdjakbfk");

            a = Console.ReadLine();

            Console.WriteLine(a);
        }
    }
}

See how similar it is? :0

Rabees
New
New

Posts : 3
Join date : 2012-02-21
Age : 21
Location : Ohio

View user profile

Back to top Go down

Re: Silent's Program Thread

Post  SilentNite17 on Sat Mar 03, 2012 11:35 pm

Huh yeah cept in the first thing, the using thing, you have to put everything that you use in C++ but in java you only have to put things that aren't already built in to the program.
avatar
SilentNite17
Admin

Posts : 21
Join date : 2012-02-20
Location : Behind you.

View user profile http://gennetdwell.forumotion.com

Back to top Go down

Re: Silent's Program Thread

Post  SilentNite17 on Tue Mar 06, 2012 12:53 am

Hey, guys!

Made a new code. This one uses java.io! (yay)

What it does is it asks you if you want to save or load passwords to a file named "passwords.txt". If it is your first time running the program, you should save. Then it will ask you for your passwords, and then it will terminate. Then, you can run the program again and say that you want to load you passwords, and it will tell you what your passwords are.

KNOWN BUGS:::

:When printing passwords, if there is nothing in a place, it will print out "null"
:Will not let me issue a command to stop it instead of just using all 10 password spaces

If you have any questions, just PM me. Still workin on the bugs, and some minor performance enhancements.

Here is the code:

Code:
import java.util.Scanner;
import java.io.*;

public class Passwords {
   
   public static void main(String[] arg) throws IOException  {
      
      String[] passwords = new String[10];
      int place;
      String pass;
      String sl;  //the variable that figures out whether you're going to save or load passwords
      
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Would you like to save or load passwords?");
      sl = scan.nextLine();
      
      switch (sl.toLowerCase())  {
      
      case "save":
         save(passwords);
           break;
      case "load":
         load(passwords);
          break;
      }
      
      
      
   }
      static void save (String[] passwords) throws IOException  {
      
      passwords = new String[10];
      int place = 0;
      String pass;
      
      Scanner scan = new Scanner(System.in);
      
      while (place<10)  {
         System.out.print("?");
         pass = scan.nextLine();
         passwords[place] = pass;
         place++;
         
         
      }
      
      FileWriter saveFile = new FileWriter("passwords.txt");
      
      place = 0;
      
      while (place<10)  {
         saveFile.write(passwords[place] + " ");
         place++;
      }
      
      saveFile.close();
         
   }

   static void load (String[] passwords) throws IOException  {
      
      int place = 0;
      
      BufferedReader saveFile = new BufferedReader(new FileReader("passwords.txt"));
      
      System.out.print("Your passwords are: ");
      
      while (place<10)  {
         passwords[place] = saveFile.readLine();
         System.out.print(passwords[place]);
         place++;
         
      }
      
   }
}

Hope you like it!

-Silent
avatar
SilentNite17
Admin

Posts : 21
Join date : 2012-02-20
Location : Behind you.

View user profile http://gennetdwell.forumotion.com

Back to top Go down

Re: Silent's Program Thread

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum