Class 1 Homework: Write a program that reads 10 double values from the user, passes them in an array to a method, and uses the method to return the sum of the elements, which should be printed.
voyinger
Apr 28, 2020
package org.jb;
import java.util.Scanner;
/*
*
* Class 1 Homework: Write a program that reads 10 double values from the user,
* passes them in an array to a metho
*
* Jerry Yi
*/
publicclass Homework1 {
publicstaticvoid main(String [] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
double sum = 0;
while (scanner.hasNextInt()) {
double x = scanner.nextDouble();
//1System.out.println(x);
count++;
sum += x;
if (count == 10) break;
}
System.out.println(sum);
}
}
angelazhong12
Apr 28, 2020
import java.util.Scanner;class Main {publicstaticdouble add (String[] splited) {int length = splited.length;double sum = 0;int index = 0;while (index <= length-1){ sum = sum + Double.parseDouble(splited[index]); index = index + 1; }return sum; }publicstaticvoid main(String[] args) { Scanner numbers = new Scanner(System.in); System.out.println("type 10 numbers delimited by space"); String ten = numbers.nextLine(); String[] splited = ten.split( " ");double sum = add( splited); System.out.println("the sum is "+sum); }}
justinwshao
Apr 30, 2020
import java.util.Scanner;import java.io.*;class Main {publicstatic StringBuffer decode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) - s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) - s - 'a') % 26 + 'a'); result.append(ch); } } return result; } publicstatic StringBuffer encode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) + s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) + s - 'a') % 26 + 'a'); result.append(ch); } } return result; }publicstaticvoid main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a string: "); String name = in.nextLine(); System.out.println("Name is: " + name); System.out.println("Enter a number: ");int num = in.nextInt(); System.out.println("Number is: " + num); System.out.println("Encode string is: " + encode(name, num)); System.out.println("Decode string is: " + decode(name, num)); }}
justinwshao
Apr 30, 2020
import java.util.Scanner;import java.io.*;class Main {publicstatic StringBuffer decode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) - s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) - s - 'a') % 26 + 'a'); result.append(ch); } } return result; } publicstatic StringBuffer encode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) + s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) + s - 'a') % 26 + 'a'); result.append(ch); } } return result; }publicstaticvoid main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a string: "); String name = in.nextLine(); System.out.println("Name is: " + name); System.out.println("Enter a number: ");int num = in.nextInt(); System.out.println("Number is: " + num); System.out.println("Encode string is: " + encode(name, num)); System.out.println("Decode string is: " + decode(name, num)); }}
helen_lan2002
Apr 30, 2020
Lucas Liu's HW
class Main {publicstatic StringBuffer encrypt(String text, int s) { StringBuffer result = new StringBuffer();for (int i = 0; i < text.length(); i++) {if (Character.isUpperCase(text.charAt(i))) {char ch = (char) (((int) text.charAt(i) + s - 65) % 26 + 65); result.append(ch); } else {char ch = (char) (((int) text.charAt(i) + s - 97) % 26 + 97); result.append(ch); } }return result; }publicstaticvoid main(String[] args) { String text = "Attack!"; System.out.println("Enter your shift:");int s = 3; System.out.println("Text : " + text); System.out.println("Shift : " + s); System.out.println("Cipher: " + encrypt(text, s)); }}
mathnerd1003
May 2, 2020
import java.util.*;import java.io.*;class Main {publicstaticvoid main(String[] args) { String str = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); String s = in.nextLine(); Scanner t = new Scanner(System.in); System.out.println("Enter an increment");int c = t.nextInt();int len = s.length();for(int i = 0; i<len; i++){char q = s.charAt(i);char w = (char)(q+c);if (q>='a' && q<='z'){if (w>'z') { w = (char)('a' + w - 'z' - 1); } }if (q>='A' && q<='Z') {if (w>'Z') { w = (char)('A' + w - 'Z' - 1); } } str = str + w; } System.out.println(str); }}
mathnerd1003
May 2, 2020
Justin Ely
cedricswang
May 2, 2020
Class 1. Write a program that reads 10 double values from the user, passes them in an array to a method, and uses the method to return the sum of the elements, which should be printed.
import java.util.Scanner;class Main {publicstaticvoid main(String[] args) {double[] arrInput = newdouble[10];Scanner scan = new Scanner(System.in); System.out.println("Please enter 10 double numbers:");for(int i=0; i<10; i++) { arrInput[i] = scan.nextDouble(); }scan.close();double sum =sumDoubles(arrInput);System.out.println("Sum: " + sum);}protectedstaticdouble sumDoubles(double[] arrDouble){double sum =0.0;for(int i=0; i<10; i++) { sum = sum + arrDouble[i]; }return sum;}}
rayj.royw
May 2, 2020
import java.util.*;import java.io.*;class Main {publicstaticvoid main(String[] args){double[] value =newdouble[10];for(int i=0; i<10; i++){ value[i]=(Math.random()*((30-20)+1))+20; Arrays.sort(value);double sum =0;for(int j =0; j<10; j++) sum +=value[j]; System.out.println(sum);}}}
Class 3 Homework: Write two methods to encode and decode a Caesar cipher where the parameters are the String input and the shift
package org.jb;
public class Homework425Jerry {
private int shift;
public Homework425Jerry(int shift) {
this.shift = shift;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Homework425Jerry app = new Homework425Jerry(-3);
String name = "JERRY";
String cipher = app.encode(name);
System.out.println(name + "--->" + cipher);
System.out.println(cipher + "--->" + name);
}
public String transform(String text, int shift) {
char [] a = new char[text.length()];
for (int i = 0; i < a.length; i++) {
int diff = text.charAt(i) - 'A' + shift;
if (diff < 0) diff+=26;
if (diff >= 26) diff%= 26;
a[i] = (char) ('A' + diff);
}
return String.valueOf(a);
}
public String encode(String text) {
return transform(text, shift);
}
public String decode(String cipher) {
return transform(cipher, -shift);
}
}
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { double[] height = new double[30]; for (int i = 0; i < 30; i++) { height[i] = (Math.random()*((80-70)+1))+70; } Arrays.sort(height); double sum = 0; for (int i = 0; i < 30; i++) sum += height[i]; System.out.println(sum/height.length); } }
package org.jb;
public class XD {
private int shift;
public XD(int shift) {
this.shift = shift;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XD app = new XD(-3);
String name = "XD";
String cipher = app.encode(name);
System.out.println(name + "--->" + cipher);
System.out.println(cipher + "--->" + name);
}
public String transform(String text, int shift) {
char [] a = new char[text.length()];
for (int i = 0; i < a.length; i++) {
int diff = text.charAt(i) - 'A' + shift;
if (diff < 0) diff+=26;
if (diff >= 26) diff%= 26;
a[i] = (char) ('A' + diff);
}
return String.valueOf(a);
}
public String encode(String text) {
return transform(text, shift);
}
public String decode(String cipher) {
return transform(cipher, -shift);
}
}
Class 2 Homework: Write two methods to encode and decode a Caesar cipher where the parameters are the String input and the shift
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter your text to encrypt"); String in_text = sc.nextLine(); System.out.println("Please enter the number of shift(1-25)"); int shift = sc.nextInt(); System.out.println("Your encrypted message is " + encrypt(in_text,shift)); Scanner sc2 = new Scanner(System.in); System.out.println("Please enter your text to decrypt"); String in_text2 = sc2.nextLine(); System.out.println("Please enter the number of shift(1-25)"); int shift2 = sc2.nextInt(); System.out.println("Your decrypted message is " + decrypt(in_text2,shift2)); } public static String encrypt(String in_text,int shift) { String ciphertext =""; char alphabet; for (int i=0; i<in_text.length(); i++){ alphabet = in_text.charAt(i); if (alphabet>= 'a'&& alphabet <='z'){ alphabet = (char) (alphabet + shift); if (alphabet>'z'){ alphabet= (char)(alphabet-26); } ciphertext = ciphertext+alphabet; } else if (alphabet>= 'A'&& alphabet <='Z'){ alphabet = (char) (alphabet + shift); if (alphabet>'Z'){ alphabet= (char)(alphabet-26); } ciphertext = ciphertext+alphabet; System.out.println("ciphertext="+ciphertext); } } return ciphertext; } public static String decrypt(String in_text,int shift) { String plaintext = ""; char alphabet; for (int i=0; i<in_text.length(); i++){ alphabet = in_text.charAt(i); if (alphabet>= 'a'&& alphabet <='z'){ alphabet = (char) (alphabet - shift); if (alphabet<'a'){ alphabet= (char)(alphabet+26); } plaintext = plaintext+alphabet; } else if (alphabet>= 'A'&& alphabet <='Z'){ alphabet = (char) (alphabet - shift); if (alphabet<'A'){ alphabet= (char)(alphabet+26); } plaintext = plaintext+alphabet; //System.out.println("plaintext="+plaintext); } } return plaintext; } }
import java.util.*; class Main { public static String encode(String str, int key){ int decimal; String encoded = ""; for (int i = 0; i < str.length(); i++){ decimal = str.charAt(i); if (decimal >= 'a' && decimal <= 'z'){ decimal = (char)(decimal+key); if (decimal > 'z'){ decimal = (char)(decimal-26); } } else if (decimal >= 'A' && decimal <= 'Z'){ decimal = (char)(decimal+key); if (decimal > 'Z'){ decimal = (char)(decimal-26); } } encoded = encoded + (char)decimal; } return encoded; } public static String decode(String str, int key){ int decimal; String decoded = ""; for (int i = 0; i < str.length(); i++){ decimal = str.charAt(i); if (decimal >= 'a' && decimal <= 'z'){ decimal = (char)(decimal-key); if (decimal < 'a'){ decimal = (char)(decimal+26); } } else if (decimal >= 'A' && decimal <= 'Z'){ decimal = (char)(decimal-key); if (decimal < 'A'){ decimal = (char)(decimal+26); } } decoded = decoded + (char)decimal; } return decoded; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a string:"); String str = in.next(); System.out.println("Enter an integer:"); int key = in.nextInt(); System.out.print("Encoded:"); System.out.println(encode(str, key)); System.out.print("Decoded:"); System.out.println(decode(str, key)); in.close(); } }
Class 1 Homework: Write a program that reads 10 double values from the user, passes them in an array to a method, and uses the method to return the sum of the elements, which should be printed.
package org.jb;
import java.util.Scanner;
/*
*
* Class 1 Homework: Write a program that reads 10 double values from the user,
* passes them in an array to a metho
*
* Jerry Yi
*/
public class Homework1 {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
double sum = 0;
while (scanner.hasNextInt()) {
double x = scanner.nextDouble();
//1System.out.println(x);
count++;
sum += x;
if (count == 10) break;
}
System.out.println(sum);
}
}
import java.util.Scanner; class Main { public static double add (String[] splited) { int length = splited.length; double sum = 0; int index = 0; while (index <= length-1){ sum = sum + Double.parseDouble(splited[index]); index = index + 1; } return sum; } public static void main(String[] args) { Scanner numbers = new Scanner(System.in); System.out.println("type 10 numbers delimited by space"); String ten = numbers.nextLine(); String[] splited = ten.split( " "); double sum = add( splited); System.out.println("the sum is "+sum); } }
import java.util.Scanner; import java.io.*; class Main { public static StringBuffer decode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) - s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) - s - 'a') % 26 + 'a'); result.append(ch); } } return result; } public static StringBuffer encode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) + s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) + s - 'a') % 26 + 'a'); result.append(ch); } } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a string: "); String name = in.nextLine(); System.out.println("Name is: " + name); System.out.println("Enter a number: "); int num = in.nextInt(); System.out.println("Number is: " + num); System.out.println("Encode string is: " + encode(name, num)); System.out.println("Decode string is: " + decode(name, num)); } }
import java.util.Scanner; import java.io.*; class Main { public static StringBuffer decode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) - s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) - s - 'a') % 26 + 'a'); result.append(ch); } } return result; } public static StringBuffer encode(String text, int s) { StringBuffer result= new StringBuffer(); for (int i=0; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char)(((int)text.charAt(i) + s - 'A') % 26 + 'A'); result.append(ch); } else { char ch = (char)(((int)text.charAt(i) + s - 'a') % 26 + 'a'); result.append(ch); } } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a string: "); String name = in.nextLine(); System.out.println("Name is: " + name); System.out.println("Enter a number: "); int num = in.nextInt(); System.out.println("Number is: " + num); System.out.println("Encode string is: " + encode(name, num)); System.out.println("Decode string is: " + decode(name, num)); } }
Lucas Liu's HW class Main { public static StringBuffer encrypt(String text, int s) { StringBuffer result = new StringBuffer(); for (int i = 0; i < text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char) (((int) text.charAt(i) + s - 65) % 26 + 65); result.append(ch); } else { char ch = (char) (((int) text.charAt(i) + s - 97) % 26 + 97); result.append(ch); } } return result; } public static void main(String[] args) { String text = "Attack!"; System.out.println("Enter your shift:"); int s = 3; System.out.println("Text : " + text); System.out.println("Shift : " + s); System.out.println("Cipher: " + encrypt(text, s)); } }
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { String str = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); String s = in.nextLine(); Scanner t = new Scanner(System.in); System.out.println("Enter an increment"); int c = t.nextInt(); int len = s.length(); for(int i = 0; i<len; i++){ char q = s.charAt(i); char w = (char)(q+c); if (q>='a' && q<='z'){ if (w>'z') { w = (char)('a' + w - 'z' - 1); } } if (q>='A' && q<='Z') { if (w>'Z') { w = (char)('A' + w - 'Z' - 1); } } str = str + w; } System.out.println(str); } }
Justin Ely
Class 1. Write a program that reads 10 double values from the user, passes them in an array to a method, and uses the method to return the sum of the elements, which should be printed.
import java.util.Scanner; class Main { public static void main(String[] args) { double[] arrInput = new double[10]; Scanner scan = new Scanner(System.in); System.out.println("Please enter 10 double numbers:"); for(int i=0; i<10; i++) { arrInput[i] = scan.nextDouble(); } scan.close(); double sum =sumDoubles(arrInput); System.out.println("Sum: " + sum); } protected static double sumDoubles(double[] arrDouble){ double sum =0.0; for(int i=0; i<10; i++) { sum = sum + arrDouble[i]; } return sum; } }
import java.util.*; import java.io.*; class Main { public static void main(String[] args){ double[] value = new double[10]; for (int i= 0; i<10; i++){ value[i] = (Math.random()*((30 -20)+1)) +20; Arrays.sort(value); double sum = 0; for(int j =0; j<10; j++) sum +=value[j]; System.out.println(sum); } } }
public class Main { private int shift; public Main(int shift) { this.shift = shift; } public static void main(String[] args) { OtherCaesarCipher app = new OtherCaesarCipher(-3); String input = "ATTACK"; String Caesar = app.encode(input); System.out.println(input + "=" + Caesar); System.out.println("&"); System.out.println(Caesar + "=" + input); } public String thing(String text, int shift) { char[] a = new char[text.length()]; for (int i = 0; i < a.length; i++) { int difference = text.charAt(i) - 'A' + shift; if (difference < 0) difference += 26; if (difference >= 26) difference %= 26; a[i] = (char) ('A' + difference); } return String.valueOf(a); } public String encode(String text) { return thing(text, shift); } public String decode(String cipher) { return thing(cipher, -shift); } }