You are on page 1of 4

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;

public class MyArrays {

public static void main(String[] args) {


// TODO Auto-generated method stub
// declares Bollywood array and initializes with 5 names
String[] Bollywood = new String[]{"Salman", "Aditya", "Arjun","Kapoor",
"Khan"};

//loops through Bollywood array


for(int i=0; i < Bollywood.length; i++)
{
//access each element in array and prints in a seperate line
System.out.println(Bollywood[i]);
}
//initializes search to Aditya
String search = "Aditya";
boolean isFound = false;
//for loop to iterate through Bollywood array to find each element
for(int i=0; i < Bollywood.length; i++)
{
//compares the element with the seacrh initialized
if(search.equals(Bollywood[i]))
{
isFound = true;
System.out.println("String found at "+(i+1));
}
}
if(!isFound)
{
System.out.println("String not found");
}

// 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");

for(int i=0; i < Ages.length; i++)


{
System.out.println(Ages[i]);
}
// Array is sorted
Arrays.sort(Ages);
//prints out sorted ages
System.out.println("After sorting the Ages are");
//loops through ages
for(int i=0; i < Ages.length; i++)
{
//prints sorted ages
System.out.println(Ages[i]);
}

}
}

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

You might also like