Java Assignment
Java Assignment
Exercise 1
ArrayDecs.java
// class ArrayDecs
public class ArrayDecs {
//main clasS
public static void main(String[] args) {
// TODO Auto-generated method stub
// declare cityPopulation with 20 elements
String cityPopulation[]= new String[20];
// declare array squad
String squad[]= new String[11];
//declare array planets and initialize all the planets
String planets[]= {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune", "Pluto"
};
Exercise 2
MyArrays.java
import java.util.Arrays;
// Array is sorted
Arrays.sort(Bollywood);
System.out.println("After sorting names the String array");
for(int i=0; i < Bollywood.length; i++)
{
//prints elements of the array after sorting
System.out.println(Bollywood[i]);
}
//declares array age and initializes with four integers
int []Ages=new int[]{56,12,32,27};
System.out.println("The Integer array");
}
}
Screenshot
Exercise 3
DNASwitch.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//DNASwitch class
public class DNASwitch {
//main class
public static void main(String[] args) throws IOException {
// inputs from buffer
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader buffer=new BufferedReader(read);
//count loops
int num=0;
String character ="";
while(num<8){//while loops will run 8 times
System.out.println("Enter a nucleotide");
char ch=buffer.readLine().charAt(0);
//switch case
switch(ch)
{
case 'a':
case 'A':
//prints Adenine when input is A or a
System.out.println("Adenine");
//it is added to the already build string
character+=ch;
break; //break after adding to a string
case 'c':
case 'C':
//prints Cytosine when input is C or c
System.out.println("Cytosine");
//it is added to the already build string
character+=ch;
break;//break after adding to a string
case 'g':
case 'G':
//prints Guanine when input is G or g
System.out.println("Guanine");
//it is added to the already build string
character+=ch;
break;//break after adding to a string
case 't':
case 'T':
//prints Thymine if input is T or t
System.out.println("Thymine");
//it is added to the already build string
character+=ch;
break;//break after adding to a string
//prints invalid when input is incorrect
default:
System.out.println("Invalid Nucleotide");
}
num++; //increase counter
}
//output the whole sequense
System.out.println("The whole sequence is: "+character);
}
}
Screenshot