Você está na página 1de 9

Programming Questions - Michael Wrona 2/11/13 #1 package homework; import java.util.

Scanner; public class ProblemOne { public static void main(String[] args) { boolean dayOfTheWeekEntered = false; while (dayOfTheWeekEntered == false) { System.out.println("Please enter a valid day of the week "); Scanner userInput = new Scanner(System.in); String dayOfTheWeek = userInput.next(); if (dayOfTheWeek.equals("Monday") | dayOfTheWeek.equals( "Tuesday") | dayOfTheWeek.equals("Wednesday") | dayOfTheWeek.equals("Thursday") | dayOfTheWeek.equals("Friday") | dayOfTheWeek.equals("Saturday") | dayOfTheWeek .equals("Sunday")){ dayOfTheWeekEntered = true; System.out.println(dayOfTheWeek.length()); } } } } #2 package homework; import java.util.Scanner; public class ProblemTwo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Principal?"); Scanner principal = new Scanner(System.in); double principalDouble = Double.parseDouble(principal.next()); System.out.println("Rate?"); Scanner rate = new Scanner(System.in); double rateDouble = Double.parseDouble(rate.next()); System.out.println("# of years?"); Scanner years = new Scanner(System.in); double yearsDouble = Double.parseDouble(years.next()); System.out.println("The simple interest is $" + (principalDouble *rateDouble*yearsDouble)); } } #3

public interface abstract abstract abstract abstract }

Operators { void Addition(int x, int y); void Subtraction(int x, int y); void Multiplication(int x, int y); void Division(int x, int y);

public class Implementation implements Operators { public void Addition(int x, int y) { System.out.println(x + y); } public void Subtraction(int x, int y) { System.out.println(x - y); } public void Multiplication(int x, int y) { System.out.println(x * y); } public void Division(int x, int y) { System.out.println(((float) x) / y); } } #4 public abstract class InheritableClass { public void CheckForUppercase(String name) { String lowercase = name.toLowerCase(); if (lowercase.equals(name)) { System.out .println("There are no uppercase letters in this string."); } else System.out.println("There are uppercase letters in this string."); } public String ConvertToUppercase(String name){ name = name.toUpperCase(); System.out.println(name); return name; } public int ConvertAndAdd(String age){ int ageInt = Integer.parseInt(age); ageInt = ageInt + 10; return ageInt; } } public class TestAbstractClass extends InheritableClass { public static void main(String[] args) { String name = args[0]; String age = args[1]; TestAbstractClass a = new TestAbstractClass(); a.CheckForUppercase(name); a.ConvertToUppercase(name);

a.ConvertAndAdd(age); System.out.println(name); System.out.println(age); } } #5 import packageForProblemFive.FloatVariables; public class ProblemFive { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub float c = FloatVariables.a + FloatVariables.b; } } package packageForProblemFive; public class FloatVariables { public static float a = 1F; public static float b = 2F; } #6 public class Fibonacci { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int x = 0; int y = 1; System.out.println(x); System.out.println(y); for (int i = 0; i < 8; i++) { int sum = x+y; System.out.println(sum); x = y; y = sum; } } } #7

public class ProblemSeven { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] oneToOneHundred = new int[100]; for (int i = 0; i < 100; i++){ oneToOneHundred[i] = i + 1; } for (int i = 0; i< 100; i++){ if ((1+i)%2==0)System.out.println(oneToOneHundred[i]); } } } #8 public class ProblemEight { public static void main(String[] args){ String workingString = "0"; for (int i = 0; i<3; i++){ if (i%2==0)workingString = workingString +"1"; else workingString = workingString + "0"; System.out.println(workingString); } } } #9 public class ProblemNine { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int x = 1; int y = 2; x = x + y; y = x - y; x = x - y; System.out.println(x + " " + y); } } #10 public class ProblemTen { public static void main(String[] args){ int x = 1; int y = 2; System.out.println(x>=y?y:x);

} } #11 import java.util.Date; public class ProblemEleven { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i = 2; switch (i) { case 1: Math.sqrt(4); break; case 2: Date dt = new Date(); System.out.print(dt.toString()); break; case 3: String stringToSplit = "I am learning Core Java"; String[] splitString = stringToSplit.split(" "); break; default: System.out.println("default message"); } } } #12 import java.io.*; public class FileRead { /** * @param args */ static FileReader fr; static BufferedReader br; public static void main(String[] args) { // TODO Auto-generated method stub try{ fr = new FileReader("Data.txt"); br = new BufferedReader(fr); int sum = 0; int counter = 0; String line = br.readLine(); while (line != null){ String[] splitLine = line.split(":"); System.out.println("Name: " + splitLine[0] + " " + splitLine[1]); System.out.println("Age: " + splitLine[2] + " ye ars");

System.out.println("State: " + splitLine[3] + " State"); sum = sum + Integer.parseInt(splitLine[2]); line = br.readLine(); counter++; } float average = ((float)sum)/((float)counter); System.out.println("The average of the ages is " + avera ge); } catch(Exception e){ e.printStackTrace(); } finally{ try { fr.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } #13 import java.io.*; public class FileWrite { /** * @param args */ static FileReader fr; static BufferedReader br; static FileWriter fw; static BufferedWriter bw; public static void main(String[] args) { // TODO Auto-generated method stub try{ fr = new FileReader("Data.txt"); br = new BufferedReader(fr); int sum = 0; int counter = 0; String line = br.readLine(); fw = new FileWriter("DataOut.txt"); bw = new BufferedWriter(fw); while (line != null){ String[] splitLine = line.split(":"); bw.write("Name: " + splitLine[0] + " " + splitLi ne[1] + "\n"); bw.write("Age: " + splitLine[2] + " years" + "\n "); bw.write("State: " + splitLine[3] + " State" + " \n");

sum = sum + Integer.parseInt(splitLine[2]); line = br.readLine(); counter++; } float average = ((float)sum)/((float)counter); bw.write("The average of the ages is " + average); bw.flush(); } catch(Exception e){ e.printStackTrace(); } finally{ try { fr.close(); br.close(); fw.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } #14 import java.util.ArrayList; public class ProblemFourteen { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) { list.add(i + 15); } int sumOfEvens = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 0) { sumOfEvens = sumOfEvens + list.get(i); } } System.out.println("The sum of the even numbers is: " + sumOfEve ns); int sumOfOdds = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 1) { sumOfOdds = sumOfOdds + list.get(i); } } System.out.println("The sum of the even numbers is: " + sumOfOdd s);

boolean prime = true; for (int i = 9; i > -1; i--) { prime = true; for (int j = 2; j < list.get(i); j++) { if (prime == true) { if (((int) list.get(i)) % j == 0) { prime = false; list.remove(i); break; } } } } System.out.println(list); } }

JDBC assignment-input import java.io.*; import java.sql.*; import java.util.Properties; public class JDBCHw { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Properties prop = new Properties(); try { FileReader fr = new FileReader("input.txt"); BufferedReader br = new BufferedReader(fr); prop.load(new FileInputStream("datasource.properties")); Class.forName(prop.getProperty("driver")); Connection con = DriverManager.getConnection( prop.getProperty("url"), prop.getPropert y("username"), ""); Statement stmt = con.createStatement(); stmt.executeUpdate("CREATE TABLE person(name VARCHAR(3), age VARCHAR(3), state VARCHAR(2), zip VARCHAR(5))"); String line = br.readLine(); while (line != null) { // split the line String[] splitLine = line.split(":"); // input the members into the database stmt.executeUpdate("INSERT INTO person VALUES('" + splitLine[0] + "','" + splitLine[1] + "','" + splitLine[2] + "','"

+ splitLine[3] + "')"); line = br.readLine(); } con.close(); } catch (Exception e) { e.printStackTrace(); } } } JDBC Assignment- output import java.sql.*; import java.io.*; import java.util.Properties; public class JDBCHw2 { /** * @param args */ static FileWriter fw; static BufferedWriter bw; public static void main(String[] args) { // TODO Auto-generated method stub try { fw = new FileWriter("output.txt"); bw = new BufferedWriter(fw); Properties prop = new Properties(); prop.load(new FileInputStream("datasource.properties")); Class.forName(prop.getProperty("driver")); Connection con = DriverManager.getConnection( prop.getProperty("url"), prop.getPropert y("username"), ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM person;" ); while (rs.next()) { StringBuffer name = new StringBuffer(rs.getStrin g("name")); String age = String state String zip = bw.write(zip ame.reverse() + "\n"); } bw.flush(); } catch (Exception e) { e.printStackTrace(); } } } rs.getString("age"); = rs.getString("state"); rs.getString("zip"); + " " + state + " " + age + " " + n

Você também pode gostar