Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: String Tokenization and Inheritance, Exams of Javascript programming

Java code examples demonstrating string tokenization using the StringTokenizer class and inheritance through class extension. The tokenization examples use different delimiters, and the inheritance examples illustrate single, hierarchical, multiple level, and use of super in the context of classes.

Typology: Exams

2021/2022

Uploaded on 09/27/2022

venice
venice 🇬🇧

4.7

(10)

216 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
String tokenization-
*********************************************
import java.util.StringTokenizer;
import java.util.Scanner;
public class Tokenization
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String tt;
tt=sc.nextLine();
StringTokenizer st = new StringTokenizer(tt);
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
*****************************************************
*****************************************************
import java.util.StringTokenizer;
import java.util.Scanner;
public class Tokenization1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String tt;
tt=sc.nextLine();
StringTokenizer st = new StringTokenizer(tt,"/");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
*****************************************************
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Programming: String Tokenization and Inheritance and more Exams Javascript programming in PDF only on Docsity!

String tokenization-


import java.util.StringTokenizer; import java.util.Scanner;

public class Tokenization { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String tt; tt=sc.nextLine();

StringTokenizer st = new StringTokenizer(tt); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }

} }


import java.util.StringTokenizer; import java.util.Scanner;

public class Tokenization { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String tt; tt=sc.nextLine();

StringTokenizer st = new StringTokenizer(tt,"/");

while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }

} }

import java.util.StringTokenizer; public class Tokenization2 { public static void main(String[] args) {

StringTokenizer st = new StringTokenizer("how/are/you","/",true);

// checking next token System.out.println("Next token is : " + st.nextToken()); System.out.println("Next token is : " + st.nextToken()); System.out.println("Next token is : " + st.nextToken()); } }


import java.util.StringTokenizer; public class Tokenization { public static void main(String[] args) { StringTokenizer st = new StringTokenizer("how are you"," "); System.out.println("Total tokens : " + st.countTokens()); } }

Hierarchical-


class Animal { void eat(){System.out.println("eating...");} }

class Dog2 extends Animal { void bark(){System.out.println("barking...");} }

class Cat extends Animal { void meow(){System.out.println("meowing...");} }

class Hierarchical { public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark(); }}

Mutilevel-


class Animal { void eat(){System.out.println("eating...");} } class Dog1 extends Animal { void bark(){System.out.println("barking...");} } class Puppy extends Dog { void weep(){System.out.println("weeping...");} }

class multiLevel { public static void main(String args[]) { Puppy d=new Puppy(); d.weep(); d.bark(); d.eat(); } }

Use of Super in constructor-


class Superclass1 { int age;

Superclass1() { this.age = 10; }

}

class Sub_class1 extends Superclass { String name; Sub_class1(int age, String name) { super(); this.name = name; }

void display() { System.out.println("The age of " + name + " is:" +age); }

public static void main(String argd[]) { Sub_class1 s = new Sub_class1(24,"Amit"); s.display(); } }

Aggregation-


class Operation { int square(int n) { return n*n; } }

class Circle{ Operation op;//aggregation double pi=3.14;

double area(int radius) { op=new Operation(); int rsquare=op.square(radius);//code reusability (i.e. delegates the method call). return pi*rsquare; }

}

public class Aggregation { public static void main(String args[]){ Circle c=new Circle(); double result=c.area(5); System.out.println(result); } }


class Address { String city,state;

Address(String city, String state) { this.city = city; this.state = state; }

}

class Dept {