0% found this document useful (0 votes)
59 views

Java Assignment

The document contains 3 Java exercises that demonstrate the use of arrays. Exercise 1 declares and initializes different array types including strings, integers, and planets. Exercise 2 includes a Bollywood names array, searches for a name, sorts the array, and prints the sorted values. It also declares and sorts an integer ages array. Exercise 3 uses a while loop to input nucleotide values, uses a switch statement to check the input and print the nucleotide type, and concatenates the values into a DNA sequence that is printed at the end.

Uploaded by

Andrew Lassia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Java Assignment

The document contains 3 Java exercises that demonstrate the use of arrays. Exercise 1 declares and initializes different array types including strings, integers, and planets. Exercise 2 includes a Bollywood names array, searches for a name, sorts the array, and prints the sorted values. It also declares and sorts an integer ages array. Exercise 3 uses a while loop to input nucleotide values, uses a switch statement to check the input and print the nucleotide type, and concatenates the values into a DNA sequence that is printed at the end.

Uploaded by

Andrew Lassia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy